博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js面向对象加构造函数模式表单验证
阅读量:4662 次
发布时间:2019-06-09

本文共 1794 字,大约阅读时间需要 5 分钟。

<!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>

转载于:https://www.cnblogs.com/xts6/p/11312279.html

你可能感兴趣的文章
VC DLL stdcall的名字分裂问题
查看>>
maven国内镜像(maven下载慢的解决方法)
查看>>
响应式导航栏
查看>>
android 查询手机已安装的第三方应用程序
查看>>
[luogu 1967] [noip 2013 货车运输] : LCA+最大生成树+并查集
查看>>
线程间通信
查看>>
编程练习:求某个数的n次方,返回其个位和十位
查看>>
Django: 页面设计,实现验证码刷新
查看>>
php和mysql中uft-8中文编码乱码的几种解决办法
查看>>
关于AJAX
查看>>
不同版本Hibernate.获取SessionFactory的方式
查看>>
数据结构之有关图的算法(图的邻接表示法)
查看>>
js checkbox
查看>>
今天自学了网页上注册某某时的倒计时设置
查看>>
linux 命令记录
查看>>
今早一来打开IDEA,Edit Configyration 找不到Tomcat
查看>>
Intro. to LDA
查看>>
jQuery基础--基本概念
查看>>
linux 卸载 umount 提示device is busy
查看>>
优先队列的一种实现方式——堆
查看>>