Skip to content

Instantly share code, notes, and snippets.

@YasinPatel
Created July 19, 2017 07:06
Show Gist options
  • Save YasinPatel/26dd835704f32e374cd1ef1451c628e7 to your computer and use it in GitHub Desktop.
Save YasinPatel/26dd835704f32e374cd1ef1451c628e7 to your computer and use it in GitHub Desktop.
Different custom validation in jquery validator
jQuery.validator.addMethod("alphanumeric", function(value, element) { // allow only alpha numric validation
return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
});
jQuery.validator.addMethod("letterwithspaces", function(value, element) { // allow only latters with space validation
return this.optional(element) || /^[a-zA-Z]+$/.test(value);
});
jQuery.validator.addMethod("imagetype", function(value, element) { // allow image validation
return this.optional(element) || /^.*\.(jpg|png|jpeg|gif)$/i.test(value);
}, "Please Select .jpg OR .png OR .gif Image");
jQuery.validator.addMethod("filetype", function(value, element) {
return this.optional(element) || /^.*\.(doc|docx|pdf)$/i.test(value);
}, "Please Select .doc OR .docx OR .pdf Image");
jQuery.validator.addMethod('filesize', function(value, element, param) {
// param = size (in bytes)
// element = element to validate (<input>)
// value = value of the element (file name)
return this.optional(element) || (element.files[0].size <= param)
});
jQuery.validator.addMethod("lettersonly", function(value, element) { // allow only latters validation
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Please enter alphanumeric letters only");
jQuery.validator.addMethod("noSpace", function(value, element) { // allow no space validation
return value.indexOf(" ") < 0 && value != "";
}, "Please enter valid account number");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment