Created
February 12, 2012 20:01
-
-
Save scottymac/1810536 to your computer and use it in GitHub Desktop.
Custom validators for jQuery Tools
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
// custom validators | |
// generic validator for required input fields to work with validator() & [placeholder] | |
$.tools.validator.fn("input[required]", "Please complete this mandatory field.", function(input, value) { | |
var pass; | |
if ((value == "") || (value == $(input).attr("placeholder"))) { | |
$(input).addClass("invalid"); | |
pass = false; | |
} else { | |
$(input).removeClass("invalid"); | |
pass = true; | |
} | |
return pass; | |
}); | |
// phone # validator | |
$.tools.validator.fn("input.phone", "Please enter a valid phone number.", function(input, value) { | |
var pass; | |
if ( /^(\+\d)*\s*((\(\d{3}\)|\d{3}\-)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/.test(value) | |
|| /[0-9]{10}/.test(value) | |
|| (value == "") | |
|| (value == $(input).attr("placeholder"))) { | |
$(input).removeClass("invalid"); | |
pass = true; | |
} else { | |
$(input).addClass("invalid"); | |
pass = false; | |
} | |
return pass; | |
}); | |
// generic validator for required input fields to work with validator() & [placeholder] | |
$.tools.validator.fn("input.url", "Please enter a valid URL.", function(input, value) { | |
var pass; | |
if ( /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/.test(value) || (value == "") || (value == $(input).attr("placeholder"))) { | |
$(input).removeClass("invalid"); | |
pass = true; | |
} /* | |
else if (value.substr(0,3) != "http") { | |
$(input).addClass("invalid"); | |
pass = false; | |
} | |
*/else { | |
$(input).addClass("invalid"); | |
pass = false; | |
} | |
return pass; | |
}); | |
// generic validator for <select> that requires an option selection | |
// for this to work the value of the first option needs to be "#" | |
$.tools.validator.fn("select[required]", "Please make a selection.", function(input, value) { | |
// check select's value | |
var pass = (value != "#") ? true : false; | |
// add "invalid" if test fails | |
pass ? $(input).removeClass("invalid") : $(input).addClass("invalid"); | |
return pass; | |
}); | |
// password check for spaces | |
$.tools.validator.fn("[type='password']", "Please do not use spaces.", function(input) { | |
var pass; | |
if ($(input).val().match(/ /g)) { | |
$(input).addClass("invalid"); | |
pass = false; | |
} else { | |
$(input).removeClass("invalid"); | |
pass = true; | |
} | |
return pass; | |
}); | |
// password matching validation, ("Value not equal with the $1 field") | |
$.tools.validator.fn("[data-equals]", "The passwords entered do not match.", function(input) { | |
var name = input.attr("data-equals"), field = this.getInputs().filter("[name=" + name + "]"); | |
return input.val() == field.val() ? true : [name]; | |
}); | |
// minlength validation (maxlength is a part of jquery tools, this is not) | |
$.tools.validator.fn("[minlength]", "Please enter a value that is at least $1 characters.", function(input) { | |
var pass, minlength = input.attr("minlength"), length = $(input).val().length; | |
if (length < minlength) { | |
$(input).addClass("invalid"); | |
pass = [minlength]; | |
} else { | |
$(input).removeClass("invalid"); | |
pass = true; | |
} | |
//console.log("minlength:",length,">",minlength,"?",pass); | |
return pass; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment