JS表单验证类.docx

上传人:b****0 文档编号:17623743 上传时间:2023-07-27 格式:DOCX 页数:45 大小:25.49KB
下载 相关 举报
JS表单验证类.docx_第1页
第1页 / 共45页
JS表单验证类.docx_第2页
第2页 / 共45页
JS表单验证类.docx_第3页
第3页 / 共45页
JS表单验证类.docx_第4页
第4页 / 共45页
JS表单验证类.docx_第5页
第5页 / 共45页
JS表单验证类.docx_第6页
第6页 / 共45页
JS表单验证类.docx_第7页
第7页 / 共45页
JS表单验证类.docx_第8页
第8页 / 共45页
JS表单验证类.docx_第9页
第9页 / 共45页
JS表单验证类.docx_第10页
第10页 / 共45页
JS表单验证类.docx_第11页
第11页 / 共45页
JS表单验证类.docx_第12页
第12页 / 共45页
JS表单验证类.docx_第13页
第13页 / 共45页
JS表单验证类.docx_第14页
第14页 / 共45页
JS表单验证类.docx_第15页
第15页 / 共45页
JS表单验证类.docx_第16页
第16页 / 共45页
JS表单验证类.docx_第17页
第17页 / 共45页
JS表单验证类.docx_第18页
第18页 / 共45页
JS表单验证类.docx_第19页
第19页 / 共45页
JS表单验证类.docx_第20页
第20页 / 共45页
亲,该文档总共45页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

JS表单验证类.docx

《JS表单验证类.docx》由会员分享,可在线阅读,更多相关《JS表单验证类.docx(45页珍藏版)》请在冰点文库上搜索。

JS表单验证类.docx

JS表单验证类

2.1表单项。

不能为空

--

functionCheckForm()

{

if(document.form.name.value.length==0){

alert("请输入您姓名!

");

document.form.name.focus();

returnfalse;

}

returntrue;

}

-->

2.2比较两个表单项的值是否相同

--

functionCheckForm()

if(document.form.PWD.value!

=document.form.PWD_Again.value){

alert("您两次输入的密码不一样!

请重新输入.");

document.ADDUser.PWD.focus();

returnfalse;

}

returntrue;

}

-->

2.3表单项只能为数字和"_",用于电话/银行帐号验证上,可扩展到域名注册等

--

functionisNumber(String)

{

varLetters="1234567890-";//可以自己增加可输入值

vari;

varc;

if(String.charAt(0)==''-'')

returnfalse;

if(String.charAt(String.length-1)==''-'')

returnfalse;

for(i=0;i

{

c=String.charAt(i);

if(Letters.indexOf(c)<0)

returnfalse;

}

returntrue;

}

functionCheckForm()

{

if(!

isNumber(document.form.TEL.value)){

alert("您的电话号码不合法!

");

document.form.TEL.focus();

returnfalse;

}

returntrue;

}

-->

 

2.4表单项输入数值/长度限定

--

functionCheckForm()

{

if(document.form.count.value>100||document.form.count.value<1)

{

alert("输入数值不能小于零大于100!

");

document.form.count.focus();

returnfalse;

}

if(document.form.MESSAGE.value.length<10)

{

alert("输入文字小于10!

");

document.form.MESSAGE.focus();

returnfalse;

}

returntrue;

}

//-->

2.5中文/英文/数字/邮件地址合法性判断

--

functionisEnglish(name)//英文值检测

{

if(name.length==0)

returnfalse;

for(i=0;i

if(name.charCodeAt(i)>128)

returnfalse;

}

returntrue;

}

functionisChinese(name)//中文值检测

{

if(name.length==0)

returnfalse;

for(i=0;i

if(name.charCodeAt(i)>128)

returntrue;

}

returnfalse;

}

functionisMail(name)//E-mail值检测

{

if(!

isEnglish(name))

returnfalse;

i=name.indexOf("at");

j=namedotlastIndexOf("at");

if(i==-1)

returnfalse;

if(i!

=j)

returnfalse;

if(i==namedotlength)

returnfalse;

returntrue;

}

functionisNumber(name)//数值检测

{

if(name.length==0)

returnfalse;

for(i=0;i

if(name.charAt(i)<"0"||name.charAt(i)>"9")

returnfalse;

}

returntrue;

}

functionCheckForm()

{

if(!

isMail(form.[email].value)){

alert("您的电子邮件不合法!

");

form.[email].focus();

returnfalse;

}

if(!

isEnglish(form.name.value)){

alert("英文名不合法!

");

form.name.focus();

returnfalse;

}

if(!

isChinese(name.value)){

alert("中文名不合法!

");

name.focus();

returnfalse;

}

if(!

isNumber(form.PublicZipCode.value)){

alert("邮政编码不合法!

");

form.PublicZipCode.focus();

returnfalse;

}

returntrue;

}

//-->

2.6限定表单项不能输入的字符

--

functioncontain(str,charset)//字符串包含测试函数

{

vari;

for(i=0;i

if(str.indexOf(charset.charAt(i))>=0)

returntrue;

returnfalse;

}

functionCheckForm()

{

if((contain(document.form.NAME.value,"%\(\)><"))||(contain(document.form.MESSAGE.value,"%\(\)><")))

{

alert("输入了非法字符");

document.form.NAME.focus();

returnfalse;

}

returntrue;

}

//-->

 

4.邮箱格式验证 

--------------------------------------- 

//函数名:

chkemail 

//功能介绍:

检查是否为EmailAddress 

//参数说明:

要检查的字符串 

//返回值:

0:

不是1:

是 

functionchkemail(a) 

{vari=a.length; 

vartemp=a.indexOf('@'); 

vartempd=a.indexOf('.'); 

if(temp>1){ 

if((i-temp)>3){ 

if((i-tempd)>0){ 

return1; 

  

return0; 

  

5.数字格式验证 

--------------------------------------- 

//函数名:

fucCheckNUM 

//功能介绍:

检查是否为数字 

//参数说明:

要检查的数字 

//返回值:

1为是数字,0为不是数字 

functionfucCheckNUM(NUM) 

vari,j,strTemp; 

strTemp="0123456789"; 

if(NUM.length==0) 

return0 

for(i=0;i

j=strTemp.indexOf(NUM.charAt(i)); 

if(j==-1) 

//说明有字符不是数字 

return0; 

//说明是数字 

return1; 

  

6.电话号码格式验证 

--------------------------------------- 

//函数名:

fucCheckTEL 

//功能介绍:

检查是否为电话号码 

//参数说明:

要检查的字符串 

//返回值:

1为是合法,0为不合法 

functionfucCheckTEL(TEL) 

vari,j,strTemp; 

strTemp="0123456789-()#"; 

for(i=0;i

j=strTemp.indexOf(TEL.charAt(i)); 

if(j==-1) 

//说明有字符不合法 

return0; 

//说明合法 

return1; 

  

7.判断输入是否为中文的函数 

--------------------------------------- 

functionischinese(s){ 

varret=true; 

for(vari=0;i

ret=ret&&(s.charCodeAt(i)>=10000); 

returnret; 

  

8.综合的判断用户输入的合法性的函数 

--------------------------------------- 

 

//限制输入字符的位数开始 

//m是用户输入,n是要限制的位数 

functionissmall(m,n) 

if((m0)) 

 { 

 return(false); 

 } 

else 

{return(true);} 

  

9.判断密码是否输入一致 

--------------------------------------- 

functionissame(str1,str2) 

if(str1==str2) 

{return(true);} 

else 

{return(false);} 

  

10.判断用户名是否为数字字母下滑线 

--------------------------------------- 

functionnotchinese(str){ 

varreg=/[^A-Za-z0-9_]/g 

 if(reg.test(str)){ 

 return(false); 

 }else{ 

return(true);} 

11.form文本域的通用校验函数

--------------------------------------- 

作用:

检测所有必须非空的input文本,比如姓名,账号,邮件地址等等。

该校验现在只针对文本域,如果要针对form里面的其他域对象,可以改变判断条件。

使用方法:

在要检测的文本域中加入title文字。

文字是在提示信息,你要提示给用户的该字段的中文名。

比如要检测用户名

html如下,当然,最好用可视化工具比如dreamweaver什么的来编辑域。

如果要检测数字类型数据的话,再把域的id统一为sz.

javascript判断日期类型比较麻烦,所以就没有做日期类型校验的程序了.高手可以补充。

程序比较草,只是提供一个思路。

抛砖引玉!

哦,对了,函数调用方法:

functiondovalidate()

{

fm=document.forms[0]//只检测一个form,如果是多个可以改变判断条件

 for(i=0;i

 { 

 //检测判断条件,根据类型不同可以修改

 if(fm[i].tagName.toUpperCase()=="INPUT"&&fm[i].type.toUpperCase()=="TEXT"&&(fm[i].title!

=""))

  

 if(fm[i].value="/blog/="")//

 {

 str_warn1=fm[i].title+"不能为空!

";

 alert(str_warn1);

 fm[i].focus();

 returnfalse; 

 }

 if(fm[i].id.toUpperCase()=="SZ")//数字校验

 {

 if(isNaN(fm[i].value))

 {str_warn2=fm[i].title+"格式不对";

 alert(str_warn2);

 fm[i].focus();

 returnfalse;

 }

 }

 }

 returntrue;

}

2>表单提交验证类 

2.1表单项不能为空

--

functionCheckForm()

if(document.form.name.value.length==0){ 

alert("请输入您姓名!

");

document.form.name.focus();

returnfalse;

}

returntrue;

}

-->

2.2比较两个表单项的值是否相同

--

functionCheckForm()

if(document.form.PWD.value!

=document.form.PWD_Again.value){ 

alert("您两次输入的密码不一样!

请重新输入.");

document.ADDUser.PWD.focus();

returnfalse;

}

returntrue;

}

-->

2.3表单项只能为数字和"_",用于电话/银行帐号验证上,可扩展到域名注册等

--

functionisNumber(String)

varLetters="1234567890-";//可以自己增加可输入值

vari;

varc;

if(String.charAt(0)=='-')

returnfalse;

if(String.charAt(String.length-1)=='-')

returnfalse;

for(i=0;i

c=String.charAt(i);

if(Letters.indexOf(c)<0)

returnfalse;

}

returntrue;

}

functionCheckForm()

if(!

isNumber(document.form.TEL.value)){ 

alert("您的电话号码不合法!

");

document.form.TEL.focus();

returnfalse;

}

returntrue;

}

-->

2.4表单项输入数值/长度限定

--

functionCheckForm() 

if(document.form.count.value>100||document.form.count.value<1)

alert("输入数值不能小于零大于100!

");

document.form.count.focus();

returnfalse;

}

if(document.form.MESSAGE.value.length<10)

alert("输入文字小于10!

");

document.form.MESSAGE.focus();

returnfalse;

}

returntrue;

}

//-->

2.5中文/英文/数字/邮件地址合法性判断

--

functionisEnglish(name)//英文值检测

if(name.length==0)

returnfalse;

for(i=0;i

if(name.charCodeAt(i)>128)

returnfalse;

}

returntrue;

}

functionisChinese(name)//中文值检测

if(name.length==0)

returnfalse;

for(i=0;i

if(name.charCodeAt(i)>128)

returntrue;

}

returnfalse;

}

functionisMail(name)//E-mail值检测

if(!

isEnglish(name))

returnfalse;

i=name.indexOf("at");

j=namedotlastIndexOf("at");

if(i==-1)

returnfalse;

if(i!

=j)

returnfalse;

if(i==namedotlength)

returnfalse;

returntrue;

}

functionisNumber(name)//数值检测

if(name.length==0)

returnfalse;

for(i=0;i

if(name.charAt(i)<"0"||name.charAt(i)>"9")

returnfalse;

}

returntrue;

}

functionCheckForm()

if(!

isMail(form.Email.value)){ 

alert("您的电子邮件不合法!

");

form.Email.focus();

returnfalse;

}

if(!

isEnglish(form.name.value)){ 

alert("英文名不合法!

");

form.name.focus();

returnfalse;

}

if(!

isChinese(name.value)){ 

alert("中文名不合法!

");

name.focus();

returnfalse;

}

if(!

isNumber(form.PublicZipCode.value)){ 

alert("邮政编码不合法!

");

form.PublicZipCode.focus();

returnfalse;

}

returntrue;

}

//-->

2.6限定表单项不能输入的字符

--

functioncontain(str,charset)//字符串包含测试函数

vari;

for(i=0;i

if(str.indexOf(charset.charAt(i))>=0)

returntrue;

returnfalse;

}

functionCheckForm()

if((contain(document.form.NAME.value,"%\(\)><"))||(contain(document.form.MESSAGE.value,"%\(\)><")))

alert("输入了非法字符");

document.form.NAME.focus();

returnfalse;

}

returntrue;

}

//-->

#2楼得分:

0回复于:

2009-07-2416:

23:

50

js验证表单大全

1.长度限制

 

2

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 农学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2