欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS+jQuery实现注册信息的验证功能

程序员文章站 2022-04-29 08:06:19
用js和jquery实现的效果是一样的。 html代码 请填写注册信息

用js和jquery实现的效果是一样的。

html代码

<legend> 请填写注册信息</legend>
  <form action="index.html" method="post">
    <table style="text-align: right;">
      <tr>
        <td>用户名:</td>
        <td><input type="text" name="username" placeholder="由英文字母和数字组成的4-16位字符,以字母开头">
        </td>
      </tr>
      <tr>
        <td>昵称:</td>
        <td><input type="text" name="nickname" placeholder="由2-6个汉字组成">
        </td>
      </tr>
      <tr>
        <td>邮箱:</td>
        <td><input type="text" name="email" placeholder="邮箱账号@域名。如good@tom.com、whj@sina.com.cn">
        </td>
      </tr>
      <tr>
        <td>密码:</td>
        <td><input type="password" name="pwd" placeholder="由英文字母和数字组成的4-10位字符">
        </td>
      </tr>
      <tr>
        <td>确认密码:</td>
        <td><input type="password" name="pwd2" placeholder="确认密码">
        </td>
      </tr>
      <tr>
        <td>手机号码:</td>
        <td><input type="text" name="phone" placeholder="手机号由11位数字组成,且以13,15,18开头">
        </td>
      </tr>
      <tr>
        <td>出生日期:</td>
        <td><input type="text" name="date" placeholder="出生日期在1990-2009之间">
      </tr>
      <tr>
        <td colspan="2" align="left"><input type="button" name="submit" value="提交"></td>
      </tr>
    </table>
  </form>
</fieldset>

css样式

 body {
      text-align: center;
      padding: 0;
      margin: 0;
    }
    fieldset {
      width: 800px;
    }
    table tr td ~ td {
      text-align: left;
      width: 600px;
    }

js代码

//验证用户名
function check_username() {
  var username = document.getelementbyid("name").value;
  var regname = /[a-za-z]\w{4,16}/
  if (username == "" || username.trim() == "") {
    document.getelementbyid("err_name").innerhtml = "请输入用户名";
    return false;
  } else if (!regname.test(username)) {
    document.getelementbyid("err_name").innerhtml = "由英文字母和数字组成的4-16位字符,以字母开头";
    return false;
  } else {
    document.getelementbyid("err_name").innerhtml = "ok!!!";
    return true;
  }
}
//验证昵称
function check_nickname() {
  var nickname = document.getelementbyid("nick").value;
  var regname = /[\u4e00-\u9fa5]{2,6}/
  if (nickname == "" || nickname.trim() == "") {
    document.getelementbyid("err_nick").innerhtml = "请输入昵称";
    return false;
  } else if (!regname.test(nickname)) {
    document.getelementbyid("err_nick").innerhtml = "由2-6个汉字组成";
    return false;
  } else {
    document.getelementbyid("err_nick").innerhtml = "ok!!!";
    return true;
  }
}
//验证邮箱
function check_email() {
  var email = document.getelementbyid("email").value;
  var regemail = /^\w+@\w+((\.\w+)+)$/;
  if (email == "" || email.trim() == "") {
    document.getelementbyid("err_email").innerhtml = "请输入邮箱";
    return false;
  } else if (!regemail.test(email)) {
    document.getelementbyid("err_email").innerhtml = "邮箱账号@域名。如good@tom.com、whj@sina.com.cn";
    return false;
  } else {
    document.getelementbyid("err_email").innerhtml = "ok!!!";
    return true;
  }
}
//验证密码
function check_pwd() {
  var pwd = document.getelementbyid("pwd").value;
  var regpwd = /^\w{4,10}$/;
  if (pwd == "" || pwd.trim() == "") {
    document.getelementbyid("err_pwd").innerhtml = "请输入密码";
    return false;
  } else if (!regpwd.test(pwd)) {
    document.getelementbyid("err_pwd").innerhtml = "格式错误";
    return false;
  } else {
    document.getelementbyid("err_pwd").innerhtml = "ok!!!";
    return true;
  }
}
//确认密码
function check_pwd2() {
  var pwd = document.getelementbyid("pwd").value;
  var pwd2 = document.getelementbyid("pwd2").value;
  if (pwd2 == "" || pwd2.trim() == "") {
    document.getelementbyid("err_pwd2").innerhtml = "请输入密码";
    return false;
  } else if (!pwd2.equals(pwd)) {
    document.getelementbyid("err_pwd2").innerhtml = "两次输入密码不一致";
    return false;
  } else {
    document.getelementbyid("err_pwd2").innerhtml = "ok!!!";
    return true;
  }
}
//验证手机号
function check_phone() {
  var phone = document.getelementbyid("phone").value;
  var regphone = /[13,15,18]\d{9}/;
  if (phone == "" || phone.trim() == "") {
    document.getelementbyid("err_phone").innerhtml = "请输入手机号";
    return false;
  } else if (!regphone.test(phone)) {
    document.getelementbyid("err_phone").innerhtml = "手机号由11位数字组成,且以13,15,18开头";
    return false;
  } else {
    document.getelementbyid("err_phone").innerhtml = "ok!!!";
    return true;
  }
}
//验证时间
function check_date() {
  var birthday = document.getelementbyid("birthday").value;
  var regdate = /[13,15,18]\d{9}/;
  if (birthday == "" || birthday.trim() == "") {
    document.getelementbyid("err_date").innerhtml = "请输入日期";
    return false;
  } else if (!regdate.test(birthday)) {
    document.getelementbyid("err_date").innerhtml = "出生日期在1990-2009之间";
    return false;
  } else {
    document.getelementbyid("err_date").innerhtml = "ok!!!";
    return true;
  }
}

jquery代码

 $(function () {
      var errmsg;
      $.each($("input"), function (i, val) {
        $(val).blur(function () {
          if ($(val).attr("name") == "username") {
            $(".namemsg").remove();
            var username = val.value;
            var regname = /[a-za-z]\w{4,16}/
            if (username == "" || username.trim() == "") {
              errmsg = "<span class='namemsg' style='color:red;'>用户名不能为空</span>";
            } else if (!regname.test(username)) {
              errmsg = "<span class='namemsg' style='color:red;'>由英文字母和数字组成的4-16位字符,以字母开头</span>";
            } else {
              errmsg = "<span class='namemsg' style='color:red;'>ok!</span>";
            }
            $(this).parent().append(errmsg);
          } else if ($(val).attr("name") == "nickname") {
            $(".nickmsg").remove();
            var nickname = val.value;
            var regname = /[\u4e00-\u9fa5]{2,6}/
            if (nickname == "" || nickname.trim() == "") {
              errmsg = "<span class='nickmsg' style='color:red;'>昵称不能为空</span>";
            } else if (!regname.test(nickname)) {
              errmsg = "<span class='nickmsg' style='color:red;'>由2-6个汉字组成</span>";
            } else {
              errmsg = "<span class='nickmsg' style='color:red;'>ok!</span>";
            }
            $(this).parent().append(errmsg);
          } else if ($(val).attr("name") == "email") {
            $(".emailmsg").remove();
            var email = val.value;
            var regemail = /^\w+@\w+((\.\w+)+)$/;
            if (email == "" || email.trim() == "") {
              errmsg = "<span class='emailmsg' style='color:red;'>邮箱不能为空</span>";
            } else if (!regemail.test(email)) {
              errmsg = "<span class='emailmsg' style='color:red;'>邮箱账号@域名。如good@tom.com、whj@sina.com.cn</span>";
            } else {
              errmsg = "<span class='emailmsg' style='color:red;'>ok!</span>";
            }
            $(this).parent().append(errmsg);
          } else if ($(val).attr("name") == "pwd") {
            $(".pwdmsg").remove();
            var pwd = val.value;
            var regpwd = /^\w{4,10}$/;
            if (pwd == "" || pwd.trim() == "") {
              errmsg = "<span class='pwdmsg' style='color:red;'>密码不能为空</span>";
            } else if (!regpwd.test(pwd)) {
              errmsg = "<span class='pwdmsg' style='color:red;'>格式错误</span>";
            } else {
              errmsg = "<span class='pwdmsg' style='color:red;'>ok!</span>";
            }
            $(this).parent().append(errmsg);
          } else if ($(val).attr("name") == "pwd2") {
            $(".pwd2msg").remove();
            var pwd2 = val.value;
            var pwd = $("input")[3].value;
            if (pwd2 == "" || pwd2.trim() == "" || !pwd2.equals(pwd)) {
              errmsg = "<span class='pwd2msg' style='color:red;'>两次输入密码不一致</span>";
            } else {
              errmsg = "<span class='pwd2msg' style='color:red;'>ok!</span>";
            }
            $(this).parent().append(errmsg);
          } else if ($(val).attr("name") == "phone") {
            $(".phonemsg").remove();
            var phone = val.value;
            var regphone = /[13,15,18]\d{9}/;
            if (phone == "" || phone.trim() == "") {
              errmsg = "<span class = 'phonemsg' style = 'color:red;' > 手机号不能为空 < /span>"
            } else if (!regphone.test(phone)) {
              errmsg = "<span class = 'phonemsg' style = 'color:red;' > 格式错误 < /span>"
            } else {
              errmsg = "<span class = 'phonemsg' style = 'color:red;' > ok! < /span>"
            }
            $(this).parent().append(errmsg);
          } else if ($(val).attr("name") == "date") {
            $(".datemsg").remove();
            var birthday = val.value;
            var regdate = /(199\d|200\d)[-/](0\d|1[0-2])[-/](0\d|[1-2]\d|30|31)/;
            if (birthday == "" || birthday.trim() == "") {
              errmsg = "<span class='datemsg' style='color:red;'>请输入生日</span>";
            } else if (!regdate.test(birthday)) {
              errmsg = "<span class='datemsg' style='color:red;'>格式错误</span>";
            } else {
              errmsg = "<span class='datemsg' style='color:red;'>ok!</span>";
            }
            $(this).parent().append(errmsg);
          }
        });
      });
    });

总结

以上所述是小编给大家介绍的js+jquery实现注册信息的验证功能,希望对大家有所帮助