Validation for no-spaces, a-zA-Z, emails added

This commit is contained in:
Dima Malishev 2011-09-02 23:15:07 +03:00
parent f65b8c618b
commit d5457b58d0
4 changed files with 80 additions and 10 deletions

View file

@ -220,7 +220,7 @@ App.HTML.Build.user_form = function(options, id)
tpl.set(':save_button', 'SAVE');
}
options = !App.Helpers.isEmpty(options) ? options : {'CONTACT':'', 'PASSWORD':'','LOGIN_NAME':'','NS':''};
options = !App.Helpers.isEmpty(options) ? options : {'CONTACT':'', 'PASSWORD':'','LOGIN_NAME':'','LNAME':'', 'FNAME':''};
tpl = App.HTML.setTplKeys(tpl, options, true);
tpl = App.HTML.Build.user_selects(tpl, options);

View file

@ -72,10 +72,11 @@ App.Model.add = function(values, source_json)
App.Helpers.Warn('Changes were not applied');
}
else {
var build_method = App.Env.getWorldName() + '_entry';
/*var build_method = App.Env.getWorldName() + '_entry';
var tpl = App.HTML.Build[build_method](values, 'new');
App.Ref.CONTENT..replaceWith(tpl);
App.Ref.CONTENT..replaceWith(tpl);*/
// todo: reply.data;
App.Pages.prepareHTML();
}
});
}

View file

@ -285,11 +285,11 @@ App.Templates.html = {
</div>\
<div class="form-row cc">\
<label for="#" class="field-label">username:</label>\
<input type="text" class="text-field" value="~!:LOGIN_NAME~!" name="LOGIN_NAME">\
<input type="text" class="text-field rule-abc rule-required" value="~!:LOGIN_NAME~!" name="LOGIN_NAME">\
</div>\
<div class="form-row pwd-box cc">\
<label for="#" class="field-label">password:</label>\
<input type="text" class="text-field password" name="PASSWORD" value="~!:PASSWORD~!">\
<input type="text" class="text-field password rule-required" name="PASSWORD" value="~!:PASSWORD~!">\
<span class="generate-pwd do_action_generate_pass">Generate</span>\
</div>\
<div class="form-row cc">\
@ -298,7 +298,7 @@ App.Templates.html = {
~!:PACKAGE_OPTIONS~!\
</select>\
</div>\
<div class="form-row cc">\
<div class="form-row cc hidden">\
<label for="#" class="field-label">shell:</label>\
<select class="not-styled" name="SHELL">\
~!:SHELL_OPTIONS~!\
@ -312,13 +312,21 @@ App.Templates.html = {
</div>\
<div class="form-row cc">\
<label for="#" class="field-label">contact email:</label>\
<input type="text" name="CONTACT" class="text-field" value="~!:CONTACT~!">\
<input type="text" name="CONTACT" class="text-field rule-email rule-required" value="~!:CONTACT~!">\
</div>\
<div class="form-row cc">\
<label for="#" class="field-label">reports:</label>\
<input type="checkbox" name="REPORTS_ENABLED" class="not-styled" value="~!:REPORTS_ENABLED~!">\
</div>\
<div class="form-row cc">\
<label for="#" class="field-label">Firstname:</label>\
<input type="text" name="FNAME" class="not-styled rule-abc rule-required" value="~!:FNAME~!">\
</div>\
<div class="form-row cc">\
<label for="#" class="field-label">Lastname:</label>\
<input type="text" name="LNAME" class="not-styled rule-abc rule-required" value="~!:LNAME~!">\
</div>\
<div class="form-row cc hidden">\
<label for="#" class="field-label">ns1:</label>\
<input type="text" name="NS" class="text-field" value="~!:NS~!">\
</div>\
@ -357,6 +365,7 @@ App.Templates.html = {
<div class="username-box">\
<span class="user">\
<span class="nickname do_action_edit">~!:LOGIN_NAME~!</span>\
<span class="role">~!:FNAME~! ~!:LNAME~!</span>\
<span class="role">(~!:ROLE~!)</span>\
</span>\
<span class="prop-box template-box">\

View file

@ -13,6 +13,39 @@ App.Validate.Is = {
}
};
App.Validate.getFieldName = function(elm)
{
fb.log(elm);
fb.warn($(elm).prev('label').text());
return ['<strong>', $(elm).prev('label').text(), '</strong>'].join('');
}
App.Validate.Rule = {
'required' : function(elm) {
if ($(elm).val().trim() == '') {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
}
return {VALID: true};
},
'no-spaces': function(elm) {
if ($(elm).val().search(/\s/) != -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' cannot contain spaces'};
}
return {VALID: true};
},
'abc': function(elm) {
if ($(elm).val().search(/[^a-zA-Z]+/) != -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters'};
}
return {VALID: true};
},
'email': function(elm) {
if ($(elm).val().search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) == -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
}
return {VALID: true};
}
}
App.Validate.form = function(world, elm)
@ -25,12 +58,24 @@ App.Validate.form = function(world, elm)
//return; // pass
}
else {
if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
App.Env.FormError.push($(field).attr('name') + ' is required');
var rules = App.Validate.getRules(field);
$(rules).each(function(i, rule)
{
fb.log('Validate with %o %o', rule, field);
if (App.Validate.Rule[rule]) {
var result = App.Validate.Rule[rule](field);
fb.log(result);
if (result.VALID == false) {
App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
form_valid = false;
}
}
})
/*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
App.Env.FormError.push($(field).attr('name') + ' is required');
form_valid = false;
}*/
}
});
return form_valid;
}
@ -49,5 +94,20 @@ App.Validate.displayFormErrors = function(world, elm)
ref.html(errors_tpl);
}
App.Validate.getRules = function(elm)
{
var rules_string = $(elm).attr('class');
var rules = [];
$(rules_string.split(/\s/)).each(function(i, str)
{
var rule = str.split('rule-');
if (rule.length > 1) {
rules[rules.length++] = rule[1];
}
});
return rules;
}