Skip to content

Instantly share code, notes, and snippets.

@dshaw002
Created September 21, 2012 15:20
Show Gist options
  • Save dshaw002/3762127 to your computer and use it in GitHub Desktop.
Save dshaw002/3762127 to your computer and use it in GitHub Desktop.
Regex Validate
$(document).ready(function(){
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
/*return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}/);*/
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(\d{3})(-\d{3})(-\d{4})$/);
}, "Please specify a valid phone number<br>Example: 204-303-3403");
jQuery.validator.addMethod("zip", function(zip, element) {
zip = zip.replace(/\s+/g, "");
return this.optional(element) || zip.length > 4 &&
zip.match(/^\d{5}([\-]\d{4})?$/);}, "Please enter a valid zip code");
$("#frm_edit_contact").validate({
rules: {
field: {
required: true,
phoneUS: true,
zip: true
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment