经常看别人的代码,感觉他们写的如此精炼。叹为观止啊。今天,我贴上我在网上找到的一个用javascript写的校验函数。希望大家喜欢。。。
function validateFunction(obj){
if(obj==null){
if(document.forms[0])
obj=document.forms[0];
else
return true;
}
var formElements=obj.elements;
var iCount;
for(iCount=0;iCount<formElements.length;iCount++){
if(((formElements[iCount].type=="text")||(formElements[iCount].type=="textarea"))&&(formElements[iCount].value!=null))
{
/**begin**/
switch(formElements[iCount].datatype)
{
case "int":{
if(!checkInteger(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
case "date":{
if(!checkDate(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
case "email":{
if(!checkEmail(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
case "float":{
if(!checkFloat(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
default:{
if((formElements[iCount].maxlength!=null)&&(!checkString(formElements[iCount],formElements[iCount].comment,formElements[iCount].maxlength)))
return false;
break;
}
}
/**end**/
}
if((formElements[iCount].notEmpity!=null)&&(!checkNotNull(formElements[iCount],formElements[iCount].comment)))
return false;
}
return true;
}
这个函数是个主要校验函数,其它的引用函数在以后陆续给出,在在所有的要校验的类型中,要有comment,和datatype属性,例如:<input type=“text“ comment=“test“ name=“text1“ datatype=“int“>,如果,datatype是char型要有maxlength这个类型,或则,无法校验char类型。这些可以通过asp,jsp脚本语言自动生成。
function validateFunction(obj){
if(obj==null){
if(document.forms[0])
obj=document.forms[0];
else
return true;
}
var formElements=obj.elements;
var iCount;
for(iCount=0;iCount<formElements.length;iCount++){
if(((formElements[iCount].type=="text")||(formElements[iCount].type=="textarea"))&&(formElements[iCount].value!=null))
{
/**begin**/
switch(formElements[iCount].datatype)
{
case "int":{
if(!checkInteger(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
case "date":{
if(!checkDate(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
case "email":{
if(!checkEmail(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
case "float":{
if(!checkFloat(formElements[iCount],formElements[iCount].comment))
return false;
break;
}
default:{
if((formElements[iCount].maxlength!=null)&&(!checkString(formElements[iCount],formElements[iCount].comment,formElements[iCount].maxlength)))
return false;
break;
}
}
/**end**/
}
if((formElements[iCount].notEmpity!=null)&&(!checkNotNull(formElements[iCount],formElements[iCount].comment)))
return false;
}
return true;
}
这个函数是个主要校验函数,其它的引用函数在以后陆续给出,在在所有的要校验的类型中,要有comment,和datatype属性,例如:<input type=“text“ comment=“test“ name=“text1“ datatype=“int“>,如果,datatype是char型要有maxlength这个类型,或则,无法校验char类型。这些可以通过asp,jsp脚本语言自动生成。
博主分享了一个用JavaScript编写的表单校验函数。该函数可对表单元素进行多种类型校验,如整数、日期、邮箱、浮点数等。所有要校验的类型需有comment和datatype属性,char类型还需maxlength属性,这些可通过asp、jsp脚本自动生成。
-主函数&spm=1001.2101.3001.5002&articleId=208419&d=1&t=3&u=6d31247292414135bad45a6c15357be5)
3152

被折叠的 条评论
为什么被折叠?



