Home > Web Front-end > JS Tutorial > body text

jquery判断输入密码两次是否相等_jquery

WBOY
Release: 2016-05-16 15:28:22
Original
2303 people have browsed it

Jquery easyui是一个非常好的ui框架,但是表单验证中没有最常用的判断两个输出框中值相等的验证,所以我做了下扩展。

$.extend($.fn.validatebox.defaults.rules, {  
  /*必须和某个字段相等*/ 
  equalTo: { 
    validator:function(value,param){ 
      return $(param[0]).val() == value; 
    }, 
    message:'字段不匹配' 
  } 
       
}); 
Copy after login

使用示例:

密码: <input id="password" name="password" validType="length[4,32]" class="easyui-validatebox" required="true" type="password" value=""/> 
<br/> 
确认密码:<input type="password" name="repassword" id="repassword" required="true" class="easyui-validatebox" validType="equalTo['#password']" invalidMessage="两次输入密码不匹配"/> 
Copy after login

将validType属性指定为equalTo['#password']即可。
当然使用这个小插件,必须要先引用jquery easyui的js库。

使用jQuery.validate验证表单中两次密码是否一致的时候遇到了一点小问题,这是我编写的代码:

$("#aspnetForm").validate({
        rules: {
          txtName: {
            required: true
          },
          txtTrueName: {
            required: true
          },
          txtPass: {
            required: true,
            minlength: 3
          },
          txtTwoPass: {
            required: true,
            minlength: 3,
            equalTo: "#txtPass"
          },
          txtEmail: {
            required: true,
            email: true
          },
          txtAddress: {
            required: true
          },
          txtPhone: {
            required: true
          }
        },
        messages: {
          txtName: {
            required: "*请输入用户名"
          },
          txtTrueName: {
            required: "*请输入姓名"
          },
          txtPass: {
            required: "*请输入密码",
            minlength: "*密码不能小于3个字符"
          },
          txtTwoPass: {
            required: "*请输入确认密码",
            minlength: "*密码不能小于3个字符",
            equalTo: "*请再次输入相同的值"
          },
          txtEmail: {
            required: "*请输入邮箱",
            email: "*请输入正确的邮箱格式"
          },
          txtAddress: {
            required: "*请输入地址"
          },
          txtPhone: {
            required: "*请输入手机号码"
          }
        }
      });
Copy after login

为什么明明我输入的两次密码是一致的还一直提示我呢?试过不同的浏览器和不同版本的validate都是如此。

不知道大家有没有遇到类似的问题,我查阅了许多文章,总结了一下解决问题的思路:

  • 先去去查看html页面的两个password型是否都赋值id了。
  • 或者检索下页面是否存在两个txtPass的id。
  • 或者你先将equalTo去掉,在重新验证下,看看是否有存在其他错误,没有在倒回来排查这个地方。

希望这篇文章可以给大家一些启发,谢谢大家的阅读,小编一定会再接再厉。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!