PageRenderTime 49ms CodeModel.GetById 23ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/jquery-validate/additional-methods.js

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