js验证表单大全.docx

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

js验证表单大全.docx

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

js验证表单大全.docx

js验证表单大全

js验证表单大全

1.长度限制

2.只能是汉字

3."只能是英文

functiononlyEng()

{

if(!

(event.keyCode>=65&&event.keyCode<=90))

event.returnvalue=false;

}

4.只能是数字

functiononlyNum()

{

if(!

((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))

//考虑小键盘上的数字键

event.returnvalue=false;

}

5.只能是英文字符和数字

6.验证油箱格式

functionisEmail(strEmail){

if(strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!

=-1)

returntrue;

else

alert("oh");

}

7.屏蔽关键字(这里屏蔽***和****)

functiontest(){

if((a.b.value.indexOf("***")==0)||(a.b.value.indexOf("****")==0)){

alert(":

)");

a.b.focus();

returnfalse;}

}

8.两次输入密码是否相同

够了吧:

屏蔽右键很酷

oncontextmenu="returnfalse"ondragstart="returnfalse"onselectstart="returnfalse"

加在body中

2.1  表单项不能为空

--

function  CheckForm()

{  

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

alert("请输入您姓名!

");

document.form.name.focus();

return  false;

}

return  true;

}

-->

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

--

function  CheckForm()

if  (document.form.PWD.value  !

=  document.form.PWD_Again.value)  {  

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

请重新输入.");

document.ADDUser.PWD.focus();

return  false;

}

return  true;

}

-->

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

--

function  isNumber(String)

{  

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

var  i;

var  c;

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

return  false;

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

return  false;

for(  i  =  0;  i  <  String.length;  i  ++  )

{  

c  =  String.charAt(  i  );

if  (Letters.indexOf(  c  )  <  0)

return  false;

}

return  true;

}

function  CheckForm()

{  

if(!

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

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

");

document.form.TEL.focus();

return  false;

}

return  true;

}

-->

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

--

function  CheckForm()  

{  

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

{  

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

");

document.form.count.focus();

return  false;

}

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

{  

alert("输入文字小于10!

");

document.form.MESSAGE.focus();

return  false;

}

return  true;

}

//-->

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

--

function  isEnglish(name)  //英文值检测

{  

if(name.length  ==  0)

return  false;

for(i  =  0;  i  <  name.length;  i++)  {  

if(name.charCodeAt(i)  >  128)

return  false;

}

return  true;

}

function  isChinese(name)  //中文值检测

{  

if(name.length  ==  0)

return  false;

for(i  =  0;  i  <  name.length;  i++)  {  

if(name.charCodeAt(i)  >  128)

return  true;

}

return  false;

}

function  isMail(name)  //  E-mail值检测

{  

if(!

  isEnglish(name))

return  false;

i  =  name.indexOf("  at  ");

j  =  name  dot  lastIndexOf("  at  ");

if(i  ==  -1)

return  false;

if(i  !

=  j)

return  false;

if(i  ==  name  dot  length)

return  false;

return  true;

}

function  isNumber(name)  //数值检测

{  

if(name.length  ==  0)

return  false;

for(i  =  0;  i  <  name.length;  i++)  {  

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

return  false;

}

return  true;

}

function  CheckForm()

{  

if(!

  isMail(form.Email.value))  {  

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

");

form.Email.focus();

return  false;

}

if(!

  isEnglish(form.name.value))  {  

alert("英文名不合法!

");

form.name.focus();

return  false;

}

if(!

  isChinese(name.value))  {  

alert("中文名不合法!

");

name.focus();

return  false;

}

if(!

  isNumber(form.PublicZipCode.value))  {  

alert("邮政编码不合法!

");

form.PublicZipCode.focus();

return  false;

}

return  true;

}

//-->

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

--

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

{  

var  i;

for(i=0;i

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

return  true;

return  false;

}

function  CheckForm()

{  

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

{  

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

document.form.NAME.focus();

return  false;

}

return  true;

}

//-->

  

1.检查一段字符串是否全由数字组成       

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

--           

functioncheckNum(str){returnstr.match(/\D/)==null}           

alert(checkNum("1232142141"))           

alert(checkNum("123214214a1"))           

//-->         

      

2.怎么判断是否是字符         

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

if(/[^\x00-\xff]/g.test(s))alert("含有汉字");          

elsealert("全是字符");      

           

3.怎么判断是否含有汉字        

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

if(escape(str).indexOf("%u")!

=-1)alert("含有汉字");         

elsealert("全是字符");           

    

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.

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

当前位置:首页 > IT计算机 > 电脑基础知识

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

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