| 1 |
jQuery.validator.addMethod("maxWords", function(value, element, params) { |
| 2 |
return this.optional(element) || value.match(/\b\w+\b/g).length < params; |
| 3 |
}, jQuery.validator.format("Please enter {0} words or less.")); |
| 4 |
|
| 5 |
jQuery.validator.addMethod("minWords", function(value, element, params) { |
| 6 |
return this.optional(element) || value.match(/\b\w+\b/g).length >= params; |
| 7 |
}, jQuery.validator.format("Please enter at least {0} words.")); |
| 8 |
|
| 9 |
jQuery.validator.addMethod("rangeWords", function(value, element, params) { |
| 10 |
return this.optional(element) || value.match(/\b\w+\b/g).length >= params[0] && value.match(/bw+b/g).length < params[1]; |
| 11 |
}, jQuery.validator.format("Please enter between {0} and {1} words.")); |
| 12 |
|
| 13 |
|
| 14 |
jQuery.validator.addMethod("letterswithbasicpunc", function(value, element) { |
| 15 |
return this.optional(element) || /^[a-z-.,()'\"\s]+$/i.test(value); |
| 16 |
}, "Letters or punctuation only please"); |
| 17 |
|
| 18 |
jQuery.validator.addMethod("alphanumeric", function(value, element) { |
| 19 |
return this.optional(element) || /^\w+$/i.test(value); |
| 20 |
}, "Letters, numbers, spaces or underscores only please"); |
| 21 |
|
| 22 |
jQuery.validator.addMethod("lettersonly", function(value, element) { |
| 23 |
return this.optional(element) || /^[a-z]+$/i.test(value); |
| 24 |
}, "Letters only please"); |
| 25 |
|
| 26 |
jQuery.validator.addMethod("nowhitespace", function(value, element) { |
| 27 |
return this.optional(element) || /^\S+$/i.test(value); |
| 28 |
}, "No white space please"); |
| 29 |
|
| 30 |
jQuery.validator.addMethod("ziprange", function(value, element) { |
| 31 |
return this.optional(element) || /^90[2-5]\d\{2}-\d{4}$/.test(value); |
| 32 |
}, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx"); |
| 33 |
|
| 34 |
/** |
| 35 |
* Return true, if the value is a valid vehicle identification number (VIN). |
| 36 |
* |
| 37 |
* Works with all kind of text inputs. |
| 38 |
* |
| 39 |
* @example <input type="text" size="20" name="VehicleID" class="{required:true,vinUS:true}" /> |
| 40 |
* @desc Declares a required input element whose value must be a valid vehicle identification number. |
| 41 |
* |
| 42 |
* @name jQuery.validator.methods.vinUS |
| 43 |
* @type Boolean |
| 44 |
* @cat Plugins/Validate/Methods |
| 45 |
*/ |
| 46 |
jQuery.validator.addMethod( |
| 47 |
"vinUS", |
| 48 |
function(v){ |
| 49 |
if (v.length != 17) |
| 50 |
return false; |
| 51 |
var i, n, d, f, cd, cdv; |
| 52 |
var LL = ["A","B","C","D","E","F","G","H","J","K","L","M","N","P","R","S","T","U","V","W","X","Y","Z"]; |
| 53 |
var VL = [1,2,3,4,5,6,7,8,1,2,3,4,5,7,9,2,3,4,5,6,7,8,9]; |
| 54 |
var FL = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2]; |
| 55 |
var rs = 0; |
| 56 |
for(i = 0; i < 17; i++){ |
| 57 |
f = FL[i]; |
| 58 |
d = v.slice(i,i+1); |
| 59 |
if(i == 8){ |
| 60 |
cdv = d; |
| 61 |
} |
| 62 |
if(!isNaN(d)){ |
| 63 |
d *= f; |
| 64 |
} |
| 65 |
else{ |
| 66 |
for(n = 0; n < LL.length; n++){ |
| 67 |
if(d.toUpperCase() === LL[n]){ |
| 68 |
d = VL[n]; |
| 69 |
d *= f; |
| 70 |
if(isNaN(cdv) && n == 8){ |
| 71 |
cdv = LL[n]; |
| 72 |
} |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
rs += d; |
| 78 |
} |
| 79 |
cd = rs % 11; |
| 80 |
if(cd == 10){cd = "X";} |
| 81 |
if(cd == cdv){return true;} |
| 82 |
return false; |
| 83 |
}, |
| 84 |
"The specified vehicle identification number (VIN) is invalid." |
| 85 |
); |
| 86 |
|
| 87 |
/** |
| 88 |
* Return true, if the value is a valid date, also making this formal check dd/mm/yyyy. |
| 89 |
* |
| 90 |
* @example jQuery.validator.methods.date("01/01/1900") |
| 91 |
* @result true |
| 92 |
* |
| 93 |
* @example jQuery.validator.methods.date("01/13/1990") |
| 94 |
* @result false |
| 95 |
* |
| 96 |
* @example jQuery.validator.methods.date("01.01.1900") |
| 97 |
* @result false |
| 98 |
* |
| 99 |
* @example <input name="pippo" class="{dateITA:true}" /> |
| 100 |
* @desc Declares an optional input element whose value must be a valid date. |
| 101 |
* |
| 102 |
* @name jQuery.validator.methods.dateITA |
| 103 |
* @type Boolean |
| 104 |
* @cat Plugins/Validate/Methods |
| 105 |
*/ |
| 106 |
jQuery.validator.addMethod( |
| 107 |
"dateITA", |
| 108 |
function(value, element) { |
| 109 |
var check = false; |
| 110 |
var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/ |
| 111 |
if( re.test(value)){ |
| 112 |
var adata = value.split('/'); |
| 113 |
var gg = parseInt(adata[0],10); |
| 114 |
var mm = parseInt(adata[1],10); |
| 115 |
var aaaa = parseInt(adata[2],10); |
| 116 |
var xdata = new Date(aaaa,mm-1,gg); |
| 117 |
if ( ( xdata.getFullYear() == aaaa ) && ( xdata.getMonth () == mm - 1 ) && ( xdata.getDate() == gg ) ) |
| 118 |
check = true; |
| 119 |
else |
| 120 |
check = false; |
| 121 |
} else |
| 122 |
check = false; |
| 123 |
return this.optional(element) || check; |
| 124 |
}, |
| 125 |
"Please enter a correct date" |
| 126 |
); |
| 127 |
|
| 128 |
jQuery.validator.addMethod("dateNL", function(value, element) { |
| 129 |
return this.optional(element) || /^\d\d?[\.\/-]\d\d?[\.\/-]\d\d\d?\d?$/.test(value); |
| 130 |
}, "Vul hier een geldige datum in." |
| 131 |
); |
| 132 |
|
| 133 |
jQuery.validator.addMethod("time", function(value, element) { |
| 134 |
return this.optional(element) || /^([01][0-9])|(2[0123]):([0-5])([0-9])$/.test(value); |
| 135 |
}, "Please enter a valid time, between 00:00 and 23:59" |
| 136 |
); |
| 137 |
|
| 138 |
/** |
| 139 |
* matches US phone number format |
| 140 |
* |
| 141 |
* where the area code may not start with 1 and the prefix may not start with 1 |
| 142 |
* allows '-' or ' ' as a separator and allows parens around area code |
| 143 |
* some people may want to put a '1' in front of their number |
| 144 |
* |
| 145 |
* 1(212)-999-2345 |
| 146 |
* or |
| 147 |
* 212 999 2344 |
| 148 |
* or |
| 149 |
* 212-999-0983 |
| 150 |
* |
| 151 |
* but not |
| 152 |
* 111-123-5434 |
| 153 |
* and not |
| 154 |
* 212 123 4567 |
| 155 |
*/ |
| 156 |
jQuery.validator.addMethod("phone", function(phone_number, element) { |
| 157 |
phone_number = phone_number.replace(/\s+/g, ""); |
| 158 |
return this.optional(element) || phone_number.length > 9 && |
| 159 |
phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/); |
| 160 |
}, "Please specify a valid phone number"); |
| 161 |
|
| 162 |
// TODO check if value starts with <, otherwise don't try stripping anything |
| 163 |
jQuery.validator.addMethod("strippedminlength", function(value, element, param) { |
| 164 |
return jQuery(value).text().length >= param; |
| 165 |
}, jQuery.validator.format("Please enter at least {0} characters")); |
| 166 |
|
| 167 |
// same as email, but TLD is optional |
| 168 |
jQuery.validator.addMethod("email2", function(value, element, param) { |
| 169 |
return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value); |
| 170 |
}, jQuery.validator.messages.email); |
| 171 |
|
| 172 |
// same as url, but TLD is optional |
| 173 |
jQuery.validator.addMethod("url2", function(value, element, param) { |
| 174 |
return this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value); |
| 175 |
}, jQuery.validator.messages.url); |
| 176 |
|
| 177 |
// NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator |
| 178 |
// Redistributed under the the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0 |
| 179 |
// Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings) |
| 180 |
jQuery.validator.addMethod("creditcardtypes", function(value, element, param) { |
| 181 |
|
| 182 |
if (/[^0-9-]+/.test(value)) |
| 183 |
return false; |
| 184 |
|
| 185 |
value = value.replace(/\D/g, ""); |
| 186 |
|
| 187 |
var validTypes = 0x0000; |
| 188 |
|
| 189 |
if (param.mastercard) |
| 190 |
validTypes |= 0x0001; |
| 191 |
if (param.visa) |
| 192 |
validTypes |= 0x0002; |
| 193 |
if (param.amex) |
| 194 |
validTypes |= 0x0004; |
| 195 |
if (param.dinersclub) |
| 196 |
validTypes |= 0x0008; |
| 197 |
if (param.enroute) |
| 198 |
validTypes |= 0x0010; |
| 199 |
if (param.discover) |
| 200 |
validTypes |= 0x0020; |
| 201 |
if (param.jcb) |
| 202 |
validTypes |= 0x0040; |
| 203 |
if (param.unknown) |
| 204 |
validTypes |= 0x0080; |
| 205 |
if (param.all) |
| 206 |
validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080; |
| 207 |
|
| 208 |
if (validTypes & 0x0001 && /^(51|52|53|54|55)/.test(value)) { //mastercard |
| 209 |
return value.length == 16; |
| 210 |
} |
| 211 |
if (validTypes & 0x0002 && /^(4)/.test(value)) { //visa |
| 212 |
return value.length == 16; |
| 213 |
} |
| 214 |
if (validTypes & 0x0004 && /^(34|37)/.test(value)) { //amex |
| 215 |
return value.length == 15; |
| 216 |
} |
| 217 |
if (validTypes & 0x0008 && /^(300|301|302|303|304|305|36|38)/.test(value)) { //dinersclub |
| 218 |
return value.length == 14; |
| 219 |
} |
| 220 |
if (validTypes & 0x0010 && /^(2014|2149)/.test(value)) { //enroute |
| 221 |
return value.length == 15; |
| 222 |
} |
| 223 |
if (validTypes & 0x0020 && /^(6011)/.test(value)) { //discover |
| 224 |
return value.length == 16; |
| 225 |
} |
| 226 |
if (validTypes & 0x0040 && /^(3)/.test(value)) { //jcb |
| 227 |
return value.length == 16; |
| 228 |
} |
| 229 |
if (validTypes & 0x0040 && /^(2131|1800)/.test(value)) { //jcb |
| 230 |
return value.length == 15; |
| 231 |
} |
| 232 |
if (validTypes & 0x0080) { //unknown |
| 233 |
return true; |
| 234 |
} |
| 235 |
return false; |
| 236 |
}, "Please enter a valid credit card number."); |
| 237 |
|