-
-
Save aonic/8429390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var validate_rules = { | |
user: function(value) { | |
var reg = /^[a-z0-9\_\-\@\.]{1,32}$/; | |
if (reg.test(value) === false) { | |
return "Invalid"; | |
} | |
}, | |
password: function(value) { | |
var reg = /^(?=[^\d_].*?\d)\w(\w|[!@#$%]){1,48}/; | |
if (reg.test(value) === false) { | |
return "Try again"; | |
} | |
}, | |
ip: function(value) { | |
value = value.split('.'); | |
if (value.length != 4) { | |
return "Malformed IP"; | |
} | |
$.each(value, function(key, value) { | |
if(value > 255 || value < 0) { | |
return "Malformed IP"; | |
} | |
}); | |
}, | |
eq: function(value, options) { | |
var compare = $('[name='+options+']').val(); | |
if (value != compare) { | |
return "Miss-match"; | |
} | |
}, | |
host: function(value) { | |
var reg = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/; | |
if (reg.test(value) === false) { | |
return "Invalid"; | |
} | |
} | |
}; | |
var failedCount = 0; | |
function processValidation(error_message, $input) { | |
if ( typeof error_message == 'undefined' || error_message == true ) { | |
return; | |
} | |
$('<b>').html(' - '+error_message).appendTo($input.siblings('label')); | |
$input.parent().addClass("has-error"); | |
failedCount++; | |
return false; | |
} | |
function processRule(rules, $input) { | |
var attr = $input.attr('validate').split(':'); //array of params | |
var rule = attr[0]; | |
var requirement = attr[1]; | |
var value = $input.val(); //link to input value | |
$input.siblings('label').children('b').remove(); //removes old error | |
$input.parent().removeClass("has-error"); //removes has-error class | |
//checks if field is required, and length | |
if (isNaN(requirement) === false && requirement && value.length < requirement) { | |
return processValidation('Must be ' + requirement + ' characters', $input); | |
} | |
//checks if empty to stop processing | |
if (isNaN(requirement) === false && value.length === 0) { | |
return; | |
} | |
if (rule in rules) { | |
return processValidation(rules[rule].apply(this, [value, requirement]), $input); | |
} | |
} | |
$.fn.validate = function(rules) { | |
failedCount = 0; | |
rules = $.extend({}, validate_rules, rules); | |
if (this.is('[validate]')) { | |
var $input = this; | |
processRule(rules, $input); | |
} else { | |
this.find('[validate]').each(function() { | |
$input = $(this); | |
processRule(rules, $input); | |
}); | |
} | |
if (failedCount === 0) { //no errors | |
return true; | |
} else { //errors | |
event.preventDefault(); //stops form processing | |
return false; | |
} | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment