Skip to content

Instantly share code, notes, and snippets.

@jessegilbride
Created September 15, 2016 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessegilbride/19d4d18db9f06eb35bbe1d57129aa43e to your computer and use it in GitHub Desktop.
Save jessegilbride/19d4d18db9f06eb35bbe1d57129aa43e to your computer and use it in GitHub Desktop.
Form validation for zip code*, using jQuery.validator.js https://jqueryvalidation.org/ * US Postal Code, 5 or 9 digits
$("form").validate({
rules: {
Zip: {zipCodeValidation: true} // hook in custom zip code validation
}
});
$.validator.addMethod("zipCodeValidation", function() {
var zipCode = $('input#zip').val();
return (/(^\d{5}$)|(^\d{5}-\d{4}$)/).test(zipCode); // returns boolean
}, "Please enter a valid US zip code (use a hyphen if 9 digits).");
$("#submitButton").on("click", function(e) {
e.preventDefault();
var form = $("#myForm");
form.validate(); // "validate" method is required for jquery.validate.js
if (infoRequestForm.valid()) {
// console.log("the form is valid.");
// ------------------------------------------------------------
//
// CODE TO SUBMIT FORM GOES HERE.
//
// ------------------------------------------------------------
$(".formContainer").hide();
$(".thankYouMessage").show();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment