USERS / CRON / IP ready. DNS (partly). JS form validators written. Added functionality

This commit is contained in:
Dima Malishev 2011-09-03 18:20:14 +03:00
parent ea5a8a2659
commit 04c7a8b545
15 changed files with 568 additions and 297 deletions

View file

@ -16,8 +16,12 @@ 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('');
var txt_label = $(elm).prev('label').text();
if (txt_label.trim() == '') {
txt_label = $(elm).parents('.field-box').select('label:first').text();
}
return ['<strong>', txt_label, '</strong>'].join('');
}
App.Validate.Rule = {
@ -27,15 +31,21 @@ App.Validate.Rule = {
}
return {VALID: true};
},
'numeric': function(elm) {
if ($(elm).val().trim() != '' && isNaN(parseInt($(elm).val(), 10))) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is incorrect'};
}
return {VALID: true};
},
'no-spaces': function(elm) {
if ($(elm).val().search(/\s/) != -1) {
if ($(elm).val().trim() != '' && $(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'};
if ($(elm).val().trim() != '' && $(elm).val().search(/[^a-zA-Z]+/) != -1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters without spaces or other symbols'};
}
return {VALID: true};
},
@ -44,6 +54,74 @@ App.Validate.Rule = {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
}
return {VALID: true};
},
'ip': function(elm) {
if ($(elm).val().trim() != '' && (/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/).test($(elm).val()) == false) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid IP value'};
}
return {VALID: true};
},
'domain': function(elm) {
if ($(elm).val().trim() != '' && (/^([a-z0-9\.])*[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/).test($(elm).val()) == false) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid domain name'};
}
return {VALID: true};
},
'ns': function(elm) {
if ($(elm).val().trim() != '' && (/^([a-z0-9\.])*[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/).test($(elm).val()) == false) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid NS name'};
}
return {VALID: true};
},
'minute': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var minute = parseInt($(elm).val(), 10);
if (minute > 60 || minute < 0) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong minute value'};
}
return {VALID: true};
},
'hour': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var hour = parseInt($(elm).val(), 10);
if (hour > 60 || hour < 0) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong hour value'};
}
return {VALID: true};
},
'wday': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var wday = parseInt($(elm).val(), 10);
if (wday > 7 || wday < 1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong week day value'};
}
return {VALID: true};
},
'month': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var month = parseInt($(elm).val(), 10);
if (month > 1 || month < 12) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong month value'};
}
return {VALID: true};
},
'day': function(elm) {
if ($(elm).val() == '*') {
return {VALID: true};
}
var day = parseInt($(elm).val(), 10);
if (day > 31 || day < 1) {
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' wrong day value'};
}
return {VALID: true};
}
}
@ -53,7 +131,12 @@ App.Validate.form = function(world, elm)
var form_valid = true;
App.Env.FormError = [];
$(elm).find('select, input, textarea').each(function(i, field)
{
{
if ($(field).attr('type') == 'checkbox') {
var value = $(field).attr('checked') ? 'on' : 'off';
$(field).val(value);
}
if ($.inArray($(field).attr('name'), ['target', 'source', 'save']) != -1) {
//return; // pass
}
@ -61,7 +144,7 @@ App.Validate.form = function(world, elm)
var rules = App.Validate.getRules(field);
$(rules).each(function(i, rule)
{
fb.log('Validate with %o %o', rule, field);
fb.log('Validate with %o %o', rule, field);
if (App.Validate.Rule[rule]) {
var result = App.Validate.Rule[rule](field);
fb.log(result);
@ -96,18 +179,24 @@ App.Validate.displayFormErrors = function(world, elm)
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;
try {
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;
}
catch(e) {
return [];
}
}