Created
January 14, 2014 08:13
-
-
Save anonymous/8414867 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($){ | |
$.fn.validate =function() { | |
var failedCount = 0 ; | |
var validateRules = { | |
user : function(value){ | |
var reg = /^[a-z0-9\_\-\@\.]{1,32}$/; | |
if (!reg.test(value)) { | |
return failedValidation("Invalid"); | |
} | |
}, | |
password : function(value){ | |
var reg = /^(?=[^\d_].*?\d)\w(\w|[!@#$%]){1,48}/; | |
if (!reg.test(value)) { | |
return failedValidation("Try again"); | |
} | |
}, | |
ip : function(value){ | |
value = value.split('.'); | |
if (value.length != 4){ | |
return failedValidation("Malformed IP"); | |
} | |
$.each(value, function(key, value){ | |
if(value > 255 || value === 0){ | |
return failedValidation("Malformed IP"); | |
} | |
}); | |
}, | |
eq : function(value, options){ | |
var compare = $('[name='+options+']').val(); | |
if (value != compare){ | |
return failedValidation("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)) { | |
return failedValidation("Invalid"); | |
} | |
} | |
}; | |
var single = function($input){ | |
var attr = $input.attr('validate').split(':'); //array of params | |
var Rule = attr[0]; | |
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(attr[1]) && attr[1] && (value.length+1) <= attr[1]){ | |
return failedValidation('Must be '+attr[1]+' characters'); | |
attr[0] = null; //sets rules to zero to end processing | |
} | |
//checks if empty to stop processing | |
if(!isNaN(attr[1]) && value.length === 0){ | |
attr[0] = null; //sets rules to zero to end processing | |
} | |
if(attr[0] !== null ){ | |
//return validateRules(attr[0], value); | |
return validateRules[ attr[0] ].apply(this, [value, attr[1]]); | |
} | |
} | |
var failedValidation = function(error_message){ | |
$('<b>').html(' - '+error_message).appendTo($input.siblings('label')); | |
$input.parent().addClass("has-error"); | |
failedCount++; | |
return false | |
} | |
if(this.is('[validate]')){ | |
var $input = this; | |
single($input); | |
}else{ | |
this.find('[validate]').each(function(){ | |
$input = $(this); | |
single($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