Skip to content

Instantly share code, notes, and snippets.

@matejb
Created October 30, 2012 12:21
Show Gist options
  • Save matejb/3979882 to your computer and use it in GitHub Desktop.
Save matejb/3979882 to your computer and use it in GitHub Desktop.
jQuery custom validate function example
$(document).ready(function(){
$.validator.addMethod('hex_format', function(value, element){
var hexregex = /^[0-9ABCDEF]{6}$/i;
return hexregex.test(value);
});
$.validator.addMethod('http_url_format', function(value, element){
if (value.length == 0)
{
return true;
}
var hexregex = /^https?\:\/\//;
return hexregex.test(value);
});
$("#mainF").validate({
errorClass: "invalid",
errorLabelContainer: $("#validationMessageBox"),
errorContainer: $("#draft"),
wrapper: "li",
rules: {
name: {required: true},
front_color: {
required: true,
hex_format: true
},
back_color: {
required: true,
hex_format: true
},
url_android: {
http_url_format: true
},
url_ios: {
http_url_format: true
}
},
messages: {
name: {required: "Name is required!"},
front_color: {
required: "Front color is required!",
hex_format: "Color must be valid hex value!"
},
back_color: {
required: "Background color is required!",
hex_format: "Color must be valid hex value!"
},
url_android: {http_url_format: "Android URL must start with http:// or https://"},
url_ios: {http_url_format: "iOS URL must start with http:// or https://"}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment