Validation.js

// Develop by AR-LEUNG
// Website : http://study.ar-leung.net

function validate(mode, form_id, class_name){
	
	var valid = true;
	var err_box = "";
	
	/*
		var message = {
			required : "This field is required.",
			digit : "Please enter only digits.",
			number : "Please enter a valid number.",
			email : "Please enter a valid email address.",
			url : "Please enter a valid URL.",
			date : "Please enter a valid date ",
			rangeLen : "Please enter a value must be {0}-{1} character(s)",
			moreLen	: "Please enter more than {0} characters.",
			leastLen : "Please enter least than {0} characters.",
			tick : "Please tick the checkbox"
		};
	*/

	// mode == 1 ? alert(message.required) : document.getElementById(item[i].id + ":e").innerHTML = message.required;
		
	var item = document.getElementById(form_id).getElementsByClassName('js');
	var isError = false;
	
	if(mode == 0){
		for(var i = 0; i <  item.length; i++){
			var element = document.getElementById(item[i].id + ":e");
			//	var checkName = (item[i].id).split("_",2);
			//	if (element === null && checkName[1] == undefined){
			if (element === null){
				var ne = document.createElement("span");
				if(class_name != null){
					ne.setAttribute("class", class_name);
				}
				ne.setAttribute("id",item[i].id + ":e");
document.getElementById(item[i].id).parentNode.insertBefore(ne,document.getElementById(item[i].id).nextSibling); 			
			}
		}
	}
		
	for(var i = 0; i <  item.length; i++){
		required	= (item[i].getAttribute("required"));
		numeric  	= (item[i].getAttribute("numeric"));
		email 		= (item[i].getAttribute("email"));
		url		= (item[i].getAttribute("url"));
		date 		= (item[i].getAttribute("date"));
		rangeLen	= (item[i].getAttribute("rangeLen"));	
		moreLen 	= (item[i].getAttribute("moreLen"));	
		leastLen 	= (item[i].getAttribute("leastLen"));	
		tick 		= (item[i].getAttribute("tick"));	
		err 		= (item[i].getAttribute("err"));	
		value 		= document.getElementById(item[i].id).value;
			
		if((required != null) && required == "true"){		
			if(value == "" || value == null){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";	
			}
		}

		if((value != null) && numeric == "int"){	
			var numericExpression = /^[0-9]+$/;
			if(!value.match(numericExpression) || ((required != null) && required == "true") ){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";	
			}	
		}
		

		if((value != null) && numeric == "double"){	
			var numericExpression = /[0-9]|\./;
			if(!value.match(numericExpression) || ((required != null) && required == "true") ){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";	
			}	
		}
		
		if((value != null) && email == "true"){	
			var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
			if(value.search(emailRegEx) == -1){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";	
			}	
		}
		
		if((value != null) && url == "true"){	
			var urlRegEx = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i;
			
			if(!urlRegEx.test(value)){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";	
			}	
		}
		
		if((value != null) && rangeLen != null){
			var lenStr = rangeLen.split(",", 2);
			
			if(value.length < parseInt(lenStr[0]) || value.length > parseInt(lenStr[1])){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";		
			}
		}		

		if((value != null) && moreLen != null){			
			if(value.length < parseInt(moreLen)){
						mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";		
			}
		}
		
		if((value != null) && leastLen != null){			
			if(value.length > parseInt(leastLen)){
					mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";		
			}
		}
		
		if(tick == "true"){
			if( document.getElementById(item[i].id).checked == false ){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";		
			}
		}
		
		if((value != null) && date != null){
			switch(date) {
				case "YYYY-DD-MM" :
					dateExpression = /^[0-9]{4}[ -](0?[1-9]|[12][0-9]|3[01])[ -](0?[1-9]|1[012])$/;
				break;
				
				case "MM-YYYY-DD" :
					dateExpression = /^(0?[1-9]|1[012])[ -][0-9]{4}[ -](0?[1-9]|[12][0-9]|3[01])$/;
				break;
				
				case "DD-YYYY-MM" :
					dateExpression = /^(0?[1-9]|[12][0-9]|3[01])[ -][0-9]{4}[ -](0?[1-9]|1[012])$/;
				break;
				
				case "MM-DD-YYYY" : 
					dateExpression = /^(0?[1-9]|1[012])[ -](0?[1-9]|[12][0-9]|3[01])[ -][0-9]{4}$/;
				break;
				
				case "DD-MM-YYYY" : 
					dateExpression = /^(0?[1-9]|[12][0-9]|3[01])[ -](0?[1-9]|1[012])[ -][0-9]{4}$/;
				break;
				
				default: // "YYYY-MM-DD"
					dateExpression = /^[0-9]{4}[ -](0?[1-9]|1[012])[ -](0?[1-9]|[12][0-9]|3[01])$/; 
			}
			
			if(!dateExpression.test(value)){
				mode == 1 ? err_box += err + "\n" : document.getElementById(item[i].id + ":e").innerHTML = err;		
				valid = false;
			}else{
				if(mode != 1)
				document.getElementById(item[i].id + ":e").innerHTML = "";	
			}		
		}
	}	

	if(valid == false){
		if(mode == 1){
			alert(err_box)
		}
		return false 
	}else{
		// document.forms[form_id].submit();
		document.getElementById(form_id).submit()
		return true 
	}			
}

TUTORIAL :

mode = 0 : Auto generated id
mode = 1 : Alert;
mode = 2-N : Add ( DIV or SPAN ) ID, Example

mode 0 :

<form method="post" id="frm">
	<input class="js" type="input" name="box" id="box" required="true" err="message">
	<input type="submit" onclick="validate(0,'frm')" name="submit" value="submit">
</form>

mode 1 :

<form method="post" id="frm">
	<input class="validate" type="input" name="box" id="box" required="true" err="message">
	<input type="submit" onclick="validate(1,'frm')" name="submit" value="submit">
</form>

mode 2 :

<form method="post" id="frm">
	<input class="validate" type="input" name="box" id="box" required="true" err="message">
	<span id="testbox_"></span>
	<input type="submit" onclick="validate(2,'frm')" name="submit" value="submit">
</form>

Remark :
required = “true", 必須輸入中文
numeric = “int", 必須輸入中整數
numeric = “double", 必須輸入小數
email = “true", 必須輸入電郵
url = “true", 必須輸入網址
rangeLen = “0, 2″ 必須輸址 0-2 的長度數字
moreLen = “5″ 須輸入網址大過 5 的長度數字
leastLen = “5″ 須輸入網址少過 5 的長度數字
tick = “true", 必須輸入剔
date = “YYYY-DD-MM", 必須輸入只定格式日期
err = “message", 必須輸入訊息

Download : http://study.ar-leung.net/file/code/validation.js

80 thoughts on “Validation.js

  1. Pingback: Buy viagra online without prescription

  2. Pingback: discount cialis

  3. Pingback: viagra cialis

  4. Pingback: cialis coupon walmart

  5. Pingback: Fda approved viagra

  6. Pingback: Viagra overnight shipping

  7. Pingback: buy cialis canada

  8. Pingback: is there a generic cialis available?

  9. Pingback: cialis black

  10. Pingback: cialis on line

  11. Pingback: cialis for sale

  12. Pingback: online pharmacy viagra

  13. Pingback: viagra for sale

  14. Pingback: viagra for sale

  15. Pingback: ed meds online

  16. Pingback: online ed medications

  17. Pingback: new ed pills

  18. Pingback: online pharmacy

  19. Pingback: pharmacy online

  20. Pingback: canadian pharmacy

  21. Pingback: generic cialis

  22. Pingback: cialis online

  23. Pingback: online vardenafil

  24. Pingback: vardenafil pills

  25. Pingback: levitra usa

  26. Pingback: casino online gambling

  27. Pingback: casino games online

  28. Pingback: buy cheap viagra

  29. Pingback: casino games

  30. Pingback: doubleu casino online casino

  31. Pingback: generic cialis tadalafil

  32. Pingback: cash advance online

  33. Pingback: online loans

  34. Pingback: cash loans

  35. Pingback: viagra prescription

  36. Pingback: buy cialis

  37. Pingback: cialis to buy

  38. Pingback: 20 cialis

  39. Pingback: 5 mg cialis

  40. Pingback: generic for cialis

  41. Pingback: online casinos

  42. Pingback: casino games online

  43. Pingback: online casino real money us

  44. Pingback: free slots

  45. Pingback: buy cheap viagra

  46. Pingback: buying viagra online

  47. Pingback: sildenafil citrate

  48. Pingback: buy cialis online

  49. Pingback: generic tadalafil

  50. Pingback: online casino real money usa

  51. Pingback: casinos online

  52. Pingback: no prescription cialis

  53. Pingback: best generic cialis

  54. Pingback: no prescription cialis

  55. Pingback: Best price for generic viagra

  56. Pingback: online viagra cheapest

  57. Pingback: purchase viagra india

  58. Pingback: viagra order online canada

  59. Pingback: buy levitra from india

  60. Pingback: viagra online without a prescription

  61. Pingback: female cialis

  62. Pingback: sildenafil online purchase in india

  63. Pingback: Overnight delivery viagra

  64. Pingback: cialis professional wikipedia

  65. Pingback: Us discount viagra overnight delivery

  66. Pingback: generic cialis

  67. Pingback: cialis lethargy

  68. Pingback: viagra buy online

  69. Pingback: Viagra overnight shipping

  70. Pingback: custom research paper service

  71. Pingback: higher english critical essay help

  72. Pingback: Best price for generic viagra

  73. Pingback: custom law essay

  74. Pingback: writing dissertation proposal

  75. Pingback: need someone to write my paper for me

  76. Pingback: thesis research

  77. Pingback: thesis writing practice

  78. Pingback: ordering cialis in canada

  79. Pingback: order viagra online

  80. Pingback: cialis canada

發表迴響