Created
September 15, 2016 21:33
-
-
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
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
$("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