JavaScript Javascript OOP表格验证器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript Javascript OOP表格验证器相关的知识,希望对你有一定的参考价值。

/*
	Author: Alvin Crespo
	Date: 2/18/2010
	
	Description: Javascript Field Validation Class
*/


function ValidateFields(pFormID){
	var aForm = document.getElementById(pFormID);
	this.errArray = new Array();//error tracker
}

/*
 * ValidateEmail
 * 
 * @id - id element of the email addres
 * 
 * Validates a given email address 
 * 
 * returns nothing
 * */
ValidateFields.prototype.ValidateEmail = function(id){

	var emailVal = document.getElementById(id).value;

	//check length of email
	if(this.ValidateLength(emailVal)){
		this.errArray.push("You must provide an email.");
		return;
	}
	else{
		//do nothing
	}
		
	//check validity of the email using regex
	var regexpr = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;	
	var emailResult = regexpr.test(emailVal);
	
	if(!emailResult){
		this.errArray.push("Your email is invalid.");
		return;
	}
	else{
		//do nothing
	}		
}
/*
 * ValidateLength()
 * 
 * @aFieldEle - element of a field
 * 
 * Validates the length of a given element
 * 
 * returns true or false
 * */
ValidateFields.prototype.ValidateLength = function(aFieldEle){
	//check that the value is greater than 0
	if(aFieldEle.length <= 0){
		return true; //less than 0
	}
	else{
		return false; //greater than 0
	}
}

/*
 * CreateErrorList()
 * 
 * @formstatid - id of a form
 * 
 * Places the errors after the form
 * 
 * returns nothing
 */
ValidateFields.prototype.CreateErrorList = function(formstatid){
	var statList = document.getElementById(formstatid).getElementsByTagName('ul')[0];
	for(var i = 1; i<=this.errArray.length; i++){
		var aLI = document.createElement('li');
		var aLIText = document.createTextNode(this.errArray[i-1]);
		aLI.appendChild(aLIText);
		statList.appendChild(aLI);
	}
}

以上是关于JavaScript Javascript OOP表格验证器的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript Javascript OOP表格验证器

JavaScript之基础-15 JavaScript OOP(概述对象模板)

JavaScript OOP Javascript模板与构造函数和继承

JavaScript对象及初识OOP

JavaScript的面向对象编程(OOP)——类

JavaScript 与 OOP