百度前端学院---耀耀学院---任务1

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title>表单验证1</title>
 6         <style>
 7             #input{
 8                 border: 1px solid #eee;
 9                 outline: none;
10                 border-radius: 5px;
11                 height: 30px;
12                 line-height: 30px;
13                  
14             }
15             #btn{
16                 outline: none;
17                 border-radius: 5px;
18                 border:1px solid #3a9;
19                 width: 50px;
20                 height:30px;
21                 cursor: pointer;
22                 text-align: center;
23             }
24         </style>
25     </head>
26     <body>
27         <form>     
28             名字:<input type="text" id="input" maxlength="50px" > 
29             <input id="btn" value="验证" type="button">
30             <div id="err"></div>
31         </form>
32     </body>
33     <script>
34         var inputs =document.getElementById("input");
35         var err =document.getElementById("err");
36         //检查字符串长度
37         function chack_length(str){
38             var len=0;
39             for(var m=0;m<str.length;m++){
40                 var reg = /^[\u4E00-\u9FA5]+$/; 
41                 if(!reg.test(str)){                
42                     len=len+1;
43                 }else{
44                     len=len+2;
45                 }
46             }
47             return len;
48         }
49         //
50         function a(v){
51             
52             if(v===""){                
53                 err.innerHTML="内容不能为空";
54                 err.style.color="red";
55                 inputs.style.borderColor="red";
56                 return;
57             }else{
58                 var numbers = chack_length(v);
59                 if(numbers<4||numbers>16){
60                     err.innerHTML="字符长度不在4-16范围内";
61                     err.style.color="red";
62                     inputs.style.borderColor="red";
63                 }else{
64                     err.innerHTML="通过验证";
65                     err.style.color="#3a9";
66                 }
67             }
68         }
69         //点击
70         btn.onclick=function sub(){    
71             var values = inputs.value;
72             a(values);
73         };
74         
75     </script>
76 </html>

 

转载于:https://www.cnblogs.com/gaoxuerong123/p/7236813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值