Login   Register  
PHP Classes
elePHPant
Icontem

File: is.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jonas Earendel  >  jForm Validator  >  is.js  >  Download  
File: is.js
Role: Auxiliary data
Content type: text/plain
Description: Validates simple values
Class: jForm Validator
Validate forms with rules defined in form template
Author: By
Last change:
Date: 1 year ago
Size: 8,610 bytes
 

Contents

Class file image Download
var is={
	email					: function(v){ // won't crunch unless I use "new RegExp"
		var reg = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$","i");
		return reg.test(v);
	}
	,url					: function(v){return /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i.test(v)}
	,integer				: function(v){return /^-?[0-9]*$/.test(v);}
	,number					: function(v){return /^[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?$/.test(v);}
	,time					: function(v){return /^(([0-1][0-9])|([1-2][0-3])):([0-5][0-9])(:[0-9][0-9])*$/.test(v);}
	,alphanumeric			: function(v){return /^[a-zA-Z0-9_]+$/.test(v);}
	,alphabetic				: function(v){return /^[a-zA-Z]+$/.test(v);}
	,week					: function(v){return /^(1[6-9]|[2-9]\d)\d{2}-(W(0[1-9]|[1-4][0-9]|5[0-3]))$/.test(v);}
	,month					: function(v){return /^(1[6-9]|[2-9]\d)\d{2}-((0[1-9]|[1][0-2]))$/.test(v);}
	,datetime				: function(v){return /^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))(T| )(([0-1][0-9])|([1-2][0-3]))(:|\.)([0-5][0-9])((:|\.)[0-9][0-9])*(Z|[+-]+([0-1][0-9]|[2][0-3]):([0-5][0-9]))$/.test(v);}
	,datetimeLocal			: function(v){return /^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))(T| )(([0-1][0-9])|([1-2][0-3]))(:|\.)([0-5][0-9])((:|\.)[0-9][0-9])*$/.test(v);}
	,ipv4					: function(v){return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)?$/.test(v);} // Thanks to Jörn Zaefferer
	,ipv6					: function(v){return /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/.test(v);} // Thanks to Jörn Zaefferer
	,zipcode				: function(v,c)
	{
		var ok=1, patterns =
		{
			 se	: /^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$/ 								// Sweden @author Tommy Ullberg
			,us	: /^(?!0{5})(\d{5})(?!-?0{4})(-?\d{4})?$/							// USA @author Justin Elsberry
			,nl : /^[1-9][0-9]{3}[ ]?(([a-rt-zA-RT-Z]{2})|([sS][^dasDAS]))$/		// Netherlands @author "Erik"
			,ch : /^[1-9][0-9][0-9][0-9]$/											// Switzerland @author Michael Freiermuth
			,gb : /^[a-z A-Z]{2}[0-9]{1,2}[a-z A-Z]{0,1}[ ]{1,2}[0-9][a-z A-Z]{2}$/	// United Kingdom @author Tony Coombe
		};

		if(c)
		{
			c = c.toLowerCase();
			if(patterns[c])
			{
				ok = patterns[c].test(v);
			}
		}
		return ok;
	}
	,tel : function(v,c)
	{
		c = c.toLowerCase();
		var ok=1, patterns =
		{
			 us	: /^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/											// USA @author Jörn Zaefferer
			,gb : /^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})|((0|\+44)7(5|6|7|8|9){1}\d{2}\s?\d{6})$/	// United Kingdom @author Jörn Zaefferer
		};
		if(patterns[c])
		{
			ok = patterns[c].test(v);
		}
		return ok;
	}
	,personnummer : function(v) // if you're not swedish, forget about this one.
	{
		var res =  /^(\d{2})((\d{2})(\d{2})(\d{2})-\d{4})$/.exec(v);
		if(res && is.date(res[1]+res[3]+'-'+res[4]+'-'+res[5]))
		{
			return is.luhn(res[2].replace('-',''));
		}
		return 0;
	}
	,organisationsnummer : function(v) // and this one.
	{
		var res = /^(\d{2})(\d{2})(\d{2})-\d{4}$/.exec(v);
		if(res && !is.date('19'+res[1]+'-'+res[2]+'-'+res[3]))
		{
			return is.luhn(res[0].replace("-",""));
		}
		return 0;
	}
	,date : function(v,format)
	{
		format = format || "yyyy-mm-dd";
		var regexps = {
			"yyyy-mm-dd" : {
				reg : /^(\d{4})-(\d{2})-(\d{2})$/,
				y:1,
				m:2,
				d:3
			}
			,"mm/dd/yyyy" : {
				reg : /^(\d{2})\/(\d{2})\/(\d{4})$/,
				m:1,
				d:2,
				y:3
			}
		};
		if(!regexps[format])
		{
			alert('js class "is" - Invalid dateformat: ' + format);
			return false;
		}
		var
			ok = 0,
			res = regexps[format].reg.exec(v);
		if(res)
		{
			var Year	= res[regexps[format].y],
				Month	= res[regexps[format].m],
				Day		= res[regexps[format].d];
			//http://www.codeproject.com/Tips/144113/JavaScript-Date-Validation
//			ok = ((Year > 1900) && (Year <= new Date().getFullYear()));
			ok = (Year > 1900);
			if (ok) {
				ok = (Month <= 12 && Month > 0);
				if (ok) {
					var LeapYear = (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0));
					if (Month == 2) {
						ok = LeapYear ? Day <= 29 : Day <= 28;
					}
					else {
						if ((Month == 4) || (Month == 6) || (Month == 9) || (Month == 11)) {
							ok = (Day > 0 && Day <= 30);
						}
						else {
							ok = (Day > 0 && Day <= 31);
						}
					}
				}
			}
		}
		return ok;
	}
	,inArray : function(v,a) // I don't want this class to depend on jQuery, so I wrote my own.
	{
		var i,res = -1;
		for(i in a)
		{
			if(a[i]==v)
			{
				res = i;
				break;
			}
		}
		return res;
	}
	,creditcard : function(v,t) // @requires creditcard.js, found at http://www.braemoor.co.uk/software/creditcard.shtml
	{
		var res=0, types = ["Visa","MasterCard","DinersClub","CarteBlanche","AmEx","Discover","JCB","enRoute","Solo","Switch","Maestro","VisaElectron","LaserCard"];
		if(t && is.inArray(t,types) != -1)
		{
			return checkCreditCard(v,t);
		}
		for(var i in types)
		{
			if(checkCreditCard(v,types[i]))
			{
				res = 1;
				break;
			}
		}
		return res;
	}

	,luhn : function(input) // from http://www.the-art-of-web.com/javascript/validate/3/
	{
		var
			sum = 0
			,i = 0
			,numdigits = input.length
			,parity = numdigits % 2;
		for(; i < numdigits; i++)
		{
			var digit = parseInt(input.charAt(i));
			if(i % 2 == parity) digit *= 2;
			if(digit > 9) digit -= 9; sum += digit;
		}
		return (sum % 10) == 0;
	}

	,color : function(v)
	{
		var colors = ["aliceblue","antiquewhite","aqua","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brown","burlywood","cadetblue","chartreuse","chocolate","coral","cornflowerblue","cornsilk","crimson","cyan","darkblue","darkcyan","darkgoldenrod","darkgray","darkgrey","darkgreen","darkkhaki","darkmagenta","darkolivegreen","darkorange","darkorchid","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkslategrey","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dimgrey","dodgerblue","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","ghostwhite","gold","goldenrod","gray","grey","green","greenyellow","honeydew","hotpink","indianred","indigo","ivory","khaki","lavender","lavenderblush","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgoldenrodyellow","lightgray","lightgrey","lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslategray","lightslategrey","lightsteelblue","lightyellow","lime","limegreen","linen","magenta","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurple","mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose","moccasin","navajowhite","navy","oldlace","olive","olivedrab","orange","orangered","orchid","palegoldenrod","palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","red","rosybrown","royalblue","saddlebrown","salmon","sandybrown","seagreen","seashell","sienna","silver","skyblue","slateblue","slategray","slategrey","snow","springgreen","steelblue","tan","teal","thistle","tomato","turquoise","violet","wheat","white","whitesmoke","yellow","yellowgreen"];
			return /^\#?[A-Fa-f0-9]{3}([A-Fa-f0-9]{3})?$/.test(v)
			|| /^rgb\(([01]?\d\d?|2[0-4]\d|25[0-5])\,([01]?\d\d?|2[0-4]\d|25[0-5])\,([01]?\d\d?|2[0-4]\d|25[0-5])\)$/.test(v)
			|| is.inArray(v.toLowerCase(), colors) !== -1
			;
	}
};//is