Last active
September 11, 2020 13:09
-
-
Save artisanalcode/f84f83feffd1fc5a535a078bb682b644 to your computer and use it in GitHub Desktop.
A method for jQuery Validation Plugin (http://jqueryvalidation.org/). It checks if a ZIP code entered contains one of a number of prefixes specified in an array. It can also check if a ZIP code is within a certain range or if it contains a specific format (e.g. USA, Canada, etc.). Client work.
This file contains hidden or 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
/* | |
Version: 1.0 | |
File: zip-validation.js | |
Author: Juan Garcia | |
Email: dev [at] soleilnoirmedia [dot] com | |
Description: zipValidation.js contains a method for jQuery Validation Plugin (http://jqueryvalidation.org/). | |
It checks if a ZIP code entered contains one of a number of prefixes specified in an array (A requirement from a client). | |
It can also check if a ZIP code is within a certain range or if it contains a specific format (e.g. USA, Canada, etc.). | |
Ideal to validate addresses for local deliveries. | |
*/ | |
$.validator.addMethod("zipcode", function(value, element) { | |
// Specify your array | |
var prefixarray = ["10", "11", "12"]; | |
// Specify the size (from x index to y index of the string, exclusive ) of the "prefix" you want to validate against | |
var prefix = value.slice(0, 2); | |
// A single condition is checked (you can fix this by nesting, looping, chaining or other means), | |
// if you uncomment an "if" statement, comment out other "if" statements. | |
// You can check for the "prefix" to exist in the specified array | |
if ($.inArray(prefix, prefixarray) > -1) { | |
return true; | |
} | |
/* Alternatively, you can specify a range, useful for US postal codes | |
* Eg. ^1[1-3]\d{3}$ Matches the range 10XXX to 15XXX (can be used with XXXXX-XXXX formated codes, | |
* where the last four numbers are irrelevant for validation purposes). | |
* Code: | |
* if (/^1[0-5]\d{3}/.test(value) ) { | |
* return true | |
* } | |
*/ | |
/* Alternatively, you can check only for a specific format. | |
* As it stands, it would check for valid US, Mexico formats: XXXXX-XXXX or XXXXX | |
* or Canadian formats XXX-XXX or XXX XXX or XXXXXX (lower and upper case). | |
* Code: | |
* if ( /\d{5}-\d{4}$|^\d{5}$|^[a-zA-Z][0-9][a-zA-Z](-| )?[0-9][a-zA-Z][0-9]$/.test(value) ) { | |
* return true | |
* } | |
* | |
*/ | |
else { | |
return false; | |
} | |
}, | |
// Depending on the type of validation you perform you can specify an error message. | |
"The ZIP code you entered is invalid / No delivery available to the specified ZIP code."); | |
// Usage example | |
$("form").validate({ | |
rules : { | |
zip : { | |
// This validation will be performed before our method; | |
// no need to go into our method if the ZIP code isn't even long enough to be valid. | |
minlength : 5, | |
// Our method. | |
zipcode : "true" | |
} | |
}, | |
messages : { | |
zip : { | |
// Our method will provide its own error message. | |
minlength : "The ZIP code you entered is too short." | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment