Js 中关于注册表单,使用onsubmit进行拦截提交,并判断用户填写的内容是否正确

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="描述">
<meta name="keywords" content="关键词">
  <title>onsubmit表单验证</title>
  <style>
     .a{
       background:yellow;
     }
     div{
        width:400px;
        height:50px;
        padding-left:50px;
     }
     form{
       background:#ccc;
       width:500px;
       height:260px;
     }
     h3{
       color:red;
       text-align:center;
     }
  </style>
  <script>
        function test1(){
        document.getElementById('name_info').innerHTML='';
        document.getElementById('psw1_info').innerHTML='';
        document.getElementById('psw2_info').innerHTML='';
       
          var user=document.getElementById('name').value;
          var mn=document.getElementById('psw1').value;
          var rmn=document.getElementById('psw2').value;
         
          if(user.length<5||user.length>11){
            document.getElementById('name_info').innerHTML='用户名必须是5-11位';
            return false;
          }else if(mn.length<6||mn.length>12){
             document.getElementById('psw1_info').innerHTML='用户名必须是6-12位';
             return false;
          }else if(mn!=rmn) {
             document.getElementById('psw2_info').innerHTML='密码和重复密码不一致';
             return false; 
          }else{
             return true;
          }
        }

  </script>
</head>
<body>
 <form  οnsubmit="return test1()"  action="xx.com" id="reg">
  <h3>欢迎注册</h3>
<div>用户名:<input type="text" id="name">
<span id="name_info" class="a"></span>
</div>
<div>密码:<input type="password" id="psw1">
<span id="psw1_info" class="a"></span>
</div>
<div>重复密码:<input type="password" id="psw2">
<span id="psw2_info" class="a"></span>
</div>
<div><input type="submit" value="注册" id="sub" ></div>
</form>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值