java各种验证

来源:互联网 发布:网络维护如何收费 编辑:程序博客网 时间:2024/06/05 19:07

1. 长度限制

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
<script>
function test()
{
if(document.a.b.value.length>50)
{
alert("不能超过50个字符!");
document.a.b.focus();
return false;
}
}
</script>
<form name=a onsubmit="return test()">
<textarea name="b"cols="40" wrap="VIRTUAL" rows="6"></textarea>
<input type="submit"name="Submit" value="check">
</form>

2. 只能是汉字

?
1
<inputonkeyup="value="/oblog/value.replace(/[^u4E00-u9FA5]/g,'')">

3. 只能是英文

?
1
2
3
4
5
6
7
<script language="javascript">
function onlyEng()
{
if(!(event.keyCode>=65&&event.keyCode<=90))
event.returnvalue=false;
}
</script>
?
1
<inputonkeydown="onlyEng();">


4. 只能是数字

?
01
02
03
04
05
06
07
08
09
10
<script language=javascript>
 
function onlyNum()
{
    if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
    //考虑小键盘上的数字键
    event.returnvalue=false;
}
 
</script>
?
1
<inputonkeydown="onlyNum();">

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

?
1
2
<inputonkeyup="value="/oblog/value.replace(/[W]/g,"'') "
  onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,''))">

6. 验证油箱格式

?
1
2
3
4
5
6
7
8
<SCRIPT LANGUAGE=javascript RUNAT=Server>
function isEmail(strEmail) {
if (strEmail.search(/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$/) != -1)
return true;
else
alert("oh");
}
</SCRIPT>
?
1
<inputtype=textonblur=isEmail(this.value)>

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

?
1
2
3
4
5
6
7
8
<script language="javascript1.2">
function test() {
if((a.b.value.indexOf ("***") == 0)||(a.b.value.indexOf ("****") == 0)){
alert(":)");
a.b.focus();
return false;}
}
</script>
?
1
2
3
4
<formname=a onsubmit="return test()">
<inputtype=textname=b>
<inputtype="submit"name="Submit"value="check">
</form>

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

?
1
2
3
4
5
<FORMMETHOD=POSTACTION="">
<inputtype="password"id="input1">
<inputtype="password"id="input2">
<inputtype="button"value="test"onclick="check()">
</FORM>
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
<script>
function check()
{
with(document.all){
if(input1.value!=input2.value)
{
alert("false")
input1.value ="";
input2.value ="";
}
else document.forms[0].submit();
}
}
</script>

够了吧,屏蔽右键很酷

?
1
oncontextmenu="return false"ondragstart="return false"onselectstart="return false"

加在body中。

 


9. 表单项不为空

?
01
02
03
04
05
06
07
08
09
10
11
12
13
<script   language="javascript">
<!--
function   CheckForm()
if   (document.form.name.value.length   ==   0)   { 
alert("请输入您姓名!");
document.form.name.focus();
return   false;
}
return   true;
}
-->
</script>

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

?
01
02
03
04
05
06
07
08
09
10
11
12
<script   language="javascript">
<!--
function   CheckForm()
if   (document.form.PWD.value   !=   document.form.PWD_Again.value)   { 
alert("您两次输入的密码不一样!请重新输入.");
document.ADDUser.PWD.focus();
return   false;
}
return   true;
}
-->
</script>

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

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script   language="javascript">
<!--
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;
}
-->
</script>

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

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<script   language="javascript">
<!--
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;
}
//-->
</script>

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

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<SCRIPT   LANGUAGE="javascript">
<!--
 
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(form.cnname.value))   { 
alert("中文名不合法!");
form.cnname.focus();
return   false;
}
if(!   isNumber(form.PublicZipCode.value))   { 
alert("邮政编码不合法!");
form.PublicZipCode.focus();
return   false;
}
return   true;
}
//-->
</SCRIPT>

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

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<script   language="javascript">
<!--
 
function   contain(str,charset)//   字符串包含测试函数
var   i;
for(i=0;i<charset.length;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;
}
//-->
</script> 

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

?
1
2
3
4
5
6
<script language="Javascript"><!--          
function checkNum(str){return str.match(/D/)==null}          
alert(checkNum("1232142141"))          
alert(checkNum("123214214a1"))          
// -->
</script>

      

16. 怎么判断是否是字符              

?
1
2
if (/[^x00-xff]/g.test(s)) alert("含有汉字");         
else alert("全是字符"); 


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

?
1
2
if (escape(str).indexOf("%u")!=-1) alert("含有汉字");        
else alert("全是字符");  

              

18. 邮箱格式验证        

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
//函数名:chkemail    
//功能介绍:检查是否为Email Address    
//参数说明:要检查的字符串    
//返回值:0:不是1:是    
function chkemail(a)    
{
      var i=a.length;    
      var temp = a.indexOf('@');    
      var tempd = a.indexOf('.');    
      if(temp > 1) {    
            if((i-temp) > 3){    
                  if((i-tempd)>0){    
                        return1;    
                  }     
            }    
      }    
      return0;    
}   

   

19. 数字格式验证         

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
//函数名:fucCheckNUM    
//功能介绍:检查是否为数字    
//参数说明:要检查的数字    
//返回值:1为是数字,0为不是数字    
function fucCheckNUM(NUM)    
{    
var i,j,strTemp;    
strTemp="0123456789";    
if ( NUM.length== 0)    
return 0    
for (i=0;i<NUM.length;i++)    
{    
j=strTemp.indexOf(NUM.charAt(i));    
if (j==-1)    
{    
//说明有字符不是数字    
return 0;    
}    
}    
//说明是数字    
return 1;    
}   

       

20. 电话号码格式验证         

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
//函数名:fucCheckTEL    
//功能介绍:检查是否为电话号码    
//参数说明:要检查的字符串    
//返回值:1为是合法,0为不合法    
function fucCheckTEL(TEL)    
{    
var i,j,strTemp;    
strTemp="0123456789-()# ";    
for (i=0;i<TEL.length;i++)    
{    
j=strTemp.indexOf(TEL.charAt(i));    
if (j==-1)    
{    
//说明有字符不合法    
return0;    
}    
}    
//说明合法    
return1;    
}  

   

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

?
1
2
3
4
5
function ischinese(s){  
var ret=true;  
for(vari=0;i<s.length;i++)  
ret=ret && (s.charCodeAt(i)>=10000);  
return ret;    

}   
   

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

?
01
02
03
04
05
06
07
08
09
10
11
12
<script language="javascript"
//限制输入字符的位数开始 
//m是用户输入,n是要限制的位数 
function issmall(m,n) 
if ((m<n) && (m>0)) 
   
   return(false); 
   
else 
   {return(true);} 
}  

 

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

?
1
2
3
4
5
6
7
function issame(str1,str2) 
if (str1==str2) 
{return(true);} 
else 
{return(false);} 

  

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

?
01
02
03
04
05
06
07
08
09
10
function notchinese(str){
var reg=/[^A-Za-z0-9_]/g
     if(reg.test(str)){
         return(false);
     }
     else
     {
         return(true);
     }
}

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

   作用:检测所有必须非空的input文本,比如姓名,账号,邮件地址等等。
   该校验现在只针对文本域,如果要针对form里面的其他域对象,可以改变判断条件。
使用方法:在要检测的文本域中加入title文字。文字是在提示信息,你要提示给用户的该字段的中文名。比如要检测用户名。

   html如下:
       <input name="txt_1" title="姓名">
   当然,最好用可视化工具比如dreamweaver什么的来编辑域。
   如果要检测数字类型数据的话,再把域的id统一为sz.
   javascript判断日期类型比较麻烦,所以就没有做日期类型校验的程序了.高手可以补充。

   函数调用方法:
       <form onsubmit="return dovalidate()">

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function dovalidate()
{
fm=document.forms[0]//只检测一个form,如果是多个可以改变判断条件
     for(i=0;i<fm.length;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();
          return false;        
          }
          if(fm[i].id.toUpperCase()=="SZ")//数字校验
          {
                  if(isNaN(fm[i].value))
                 { str_warn2=fm[i].title+"格式不对";
                 alert(str_warn2);
                 fm[i].focus();
                  returnfalse;
                  }
         }
     }
     returntrue;
}

 

表单提交验证类
———————————

26. 表单项不能为空

 

?
01
02
03
04
05
06
07
08
09
10
11
12
13
<script language="javascript">
<!--
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert("请输入您姓名!");
document.form.name.focus();
return false;
}
return true;
}
-->
</script>

 

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

?
01
02
03
04
05
06
07
08
09
10
11
12
<script language="javascript">
<!--
function CheckForm()
if (document.form.PWD.value != document.form.PWD_Again.value) {
alert("您两次输入的密码不一样!请重新输入.");
document.ADDUser.PWD.focus();
return false;
}
return true;
}
-->
</script>

 

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

 

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<script language="javascript">
<!--
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;
}
-->
</script>

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

 

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
<script language="javascript">
<!--
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;
}
//-->
</script>

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

 

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<SCRIPT LANGUAGE="javascript">
<!--
function isEnglish(name)//英文值检测
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
return false;
}
returntrue;
}
 
function isChinese(name)//中文值检测
{
if(name.length == 0)
returnfalse;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
returntrue;
}
returnfalse;
}
 
function isMail(name)// E-mail值检测
{
if(! isEnglish(name))
returnfalse;
i = name.indexOf(" at ");
j = name dot lastIndexOf(" at ");
if(i == -1)
returnfalse;
if(i != j)
returnfalse;
if(i == name dot length)
returnfalse;
returntrue;
}
 
function isNumber(name)//数值检测
{
if(name.length == 0)
returnfalse;
for(i = 0; i < name.length; i++) {
if(name.charAt(i) <"0" || name.charAt(i) >"9")
returnfalse;
}
returntrue;
}
 
function CheckForm()
{
if(! isMail(form.Email.value)) {
alert("您的电子邮件不合法!");
form.Email.focus();
returnfalse;
}
if(! isEnglish(form.name.value)) {
alert("英文名不合法!");
form.name.focus();
returnfalse;
}
if(! isChinese(form.cnname.value)) {
alert("中文名不合法!");
form.cnname.focus();
returnfalse;
}
if(! isNumber(form.PublicZipCode.value)) {
alert("邮政编码不合法!");
form.PublicZipCode.focus();
returnfalse;
}
returntrue;
}
//-->
</SCRIPT>

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

 

?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script language="javascript">
<!--
function contain(str,charset)// 字符串包含测试函数
{
var i;
for(i=0;i<charset.length;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;
}
//-->
</script>
0 0