Last active
August 29, 2015 14:03
-
-
Save varver/1b2d84f97364b7b52bdb to your computer and use it in GitHub Desktop.
javascript plugin for form validation . Easily add custom validations in less than a minute .
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
// plugin to be used for validating widgets for forms or anything with defined rules | |
/* | |
//////////////////////////////////////////////////////////////////////////////////////// | |
How to use ? | |
1) Add class = "validator" to element which has to be validated. | |
2) Add comma separated validation constraints in attribute called validator in that element like : validator="required,url" | |
(validators are defined in function called "Check_Validation" you can add more validators here .) | |
Example = <input name="url" value="http://google.com" class="validator" , validator="required,url"> | |
In above example two types of validation check will be made : "required" and "url" . | |
You can add more validation checks in "Check_Validation" function . | |
After adding all validations , you need to initiate validation for the form , you can do it in a way mentioned below | |
Initiate Example (on click of submit button call this initializer ): | |
$("form").validateNow() ; | |
or | |
$("form#myform").validateNow(); | |
or | |
$("form")[0].validateNow(); | |
//////////////////////////////////////////////////////////////////////////////////////// | |
*/ | |
(function($){ | |
$.fn.validateNow = function(options) { | |
// it will be used to check property or attribute of element to be validated | |
optional_attribute = (typeof val === "undefined") ? "val" : optional_attribute; | |
var error_count = 0 ; | |
var all_elems = $(this).find(".validator"); | |
$(".error_red").removeClass( "error_red" ); | |
$(".error_back").removeClass( "error_back" ); | |
$(all_elems).each(function(index) { | |
if($(this).attr("validator")){ | |
var str = $(this).attr("validator").trim().toLowerCase().replace(" ", ""); | |
var parts = str.split(","); | |
// specify value to be validated | |
var current_val = "" | |
if(optional_attribute == "prop"){current_val = $(this).prop("value");} | |
else if(optional_attribute == "attr"){current_val = $(this).attr("value");} | |
else if(optional_attribute == "val"){current_val = $(this).val();} | |
var length = current_val.length; | |
// if current value is empty and it has required constraint then return immediately. | |
if(parts.indexOf("required") > -1){ | |
parts.splice(parts.indexOf("required"),1); // remove "required" from list // | |
if (current_val.length > 0){ length = current_val.length;} | |
else { | |
error_count += 1 ; | |
ReturnHandler(this,false,"The Value is Required",length); | |
return false;} | |
} | |
// Now run validation check for every constraint | |
if(length > 0) { | |
for (i = 0 ; i < parts.length ; i++) { | |
var resp = Check_Validation(parts[i],current_val); | |
if (resp[0] == false){ | |
error_count += 1 ; | |
ReturnHandler(this,resp[0],resp[1],length); | |
return false; | |
} | |
} | |
} | |
} | |
}); | |
if(error_count > 0){return false ;} | |
return true ; | |
}; | |
function ReturnHandler(obj,status,message,length){ | |
// now do appropriate thing to make this thing display an error. | |
alert(message); | |
if (length > 0){$(obj).addClass("error_red");} | |
else {$(obj).addClass("error_back");} | |
} | |
function Check_Validation(check,value){ | |
var status = true; | |
var message = "" ; | |
// add all checks here , just follow the method given below to add a new check. | |
if(check == "url"){ | |
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ | |
status = regexp.test(value); // set true or false | |
message = "please enter a valid url"; | |
} | |
// you can add more checks like this : | |
else if(check == ""){} | |
return [status,message] | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment