zoukankan      html  css  js  c++  java
  • js面向对象加构造函数模式表单验证

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>表单验证</title>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    </head>
    <body>
      <div>
        <input type="text" id="userName" value="6">
        <input type="password" id="password">
        <button id="button">提交</button>
      </div>
      <script>
        var m={
          userName:$("#userName").val(),
          password:$("#password").val(),
          userTest:/^[a-zA-Z]{1}[A-Z|a-z|0-9]{5,29}/,
          passwordTest:/^(w){6,20}$/,//6-20个字母
          test:function(testDate, testReg,testName){
            this.testDate = testDate;
            this.testReg = testReg;
            this.testName = testName;
            this.testFormData=function(){
              if(this.testDate==''){
                alert(this.testName+'不能为空,请输入')
                return false;
              }else if(!this.testReg.test(this.testDate)){
                alert(testName+'格式不正确,请重新输入')
                return false;
              }else{
                return true;
              }
            }
          },
            page:function(){
                          alert('登录成功,欢迎来到百度')
             window.location='https://www.baidu.com'
          },
              commit:function(){
            this.userName=$("#userName").val();
            this.password=$("#password").val();
            var test=this.test;
            var userNameTest=new test(this.userName,this.userTest,'用户名');
            var passwordTest=new test(this.password,this.passwordTest,'密码');
            var userNameV=userNameTest.testFormData()
            console.log(userNameV)
            if(!userNameV){//如果用户名不规范 就直接退出运行 不进行密码验证
              return;
            }else{
              var passwordV=passwordTest.testFormData()
            }
            if(userNameV&&passwordV){
              this.page();
            }
         }
    }

    $('#button').click(function(){
      m.commit()
    })
    </script>
    </body>
    </html>

  • 相关阅读:
    消息中间件(一)MQ详解及四大MQ比较
    SIP协议
    PAT (Basic Level) Practice 1008 数组元素循环右移问题
    LeetCode-Algorithms 1. 两数之和
    PAT (Basic Level) Practice 1040 有几个PAT
    PAT (Basic Level) Practice 1023 组个最小数
    PAT (Basic Level) Practice 1021 个位数统计
    PAT (Basic Level) Practice 1007 素数对猜想
    PAT (Basic Level) Practice 1006 换个格式输出整数
    PAT (Basic Level) Practice 1004 成绩排名
  • 原文地址:https://www.cnblogs.com/xts6/p/11312279.html
Copyright © 2011-2022 走看看