【问题标题】:jQuery adding custom password rule not workingjQuery添加自定义密码规则不起作用
【发布时间】:2014-03-21 11:12:41
【问题描述】:

我是 jQuery 新手,我正在使用 jQuery 表单验证器来验证我的表单..

问题是,我必须验证密码,因为它必须包含至少一位数字..

我在谷歌上搜索,我在这里找到了例子Link to forum.jquery

这是我的 HTML 代码的 sn-p

  <form  name="f2" id="f2" method="post" action="hello">
     <table>
      <tr>
             <th>
                 Password
             </th>
              <td>
             <input type="password" name="pwd" id="pwd"  placeholder="Enter Password" class="text_box">
              </td>
      </tr>
      <tr>
           <td>
              <button type="submit" class="submit-button">Register</button>
           </td>
      </tr>

     </table>
    </form>

    //jquery script for form validation

      <script>
        $(document).ready(function(){
            $("#f2").validate({
                debug: false,
                rules: { 

                 pwd:{
                        required:true,
                        minlength:5
                    }

                 },
                messages:
                    {
                      pwd:{
                        required:"Please enter password",
                        minlength:"Please enter minimum characters"
                    }
                 }
             });
        });
    </script>

在我将这一行添加到上面的代码规则之前,一切正常

pwd:{
    required:true,
    minlength:5,
    mypassword: true
}

我包含了包含以下方法的附加脚本文件

jQuery.validator.addMethod('mypassword', function(value, element) 
{
    return this.optional(element) || (value.match(/[a-zA-Z]/) && value.match(/[0-9]/));
},
'Password must contain at least one numeric and one alphabetic character.');

尝试执行上述代码后,我在浏览器控制台中收到以下错误消息:

未捕获的类型错误:无法调用未定义的方法“调用” js/jqueryvalidation.js

为什么附加方法不起作用?

任何帮助或建议将不胜感激。

【问题讨论】:

  • 你给mypassword添加了消息吗?
  • 否。实际上附加方法会在条件为假时返回消息
  • 什么是$("#f2")请提供jsfiddle
  • @dholakiyaankit 我不知道如何创建小提琴.. 等我提供我的 sn-p html

标签: javascript jquery validation jqueryform


【解决方案1】:

请查看提琴手链接中的示例:http://jsfiddle.net/NaaEw/

    jQuery.validator.addMethod('mypassword', function(value, element) 
    {
       return this.optional(element) || (value.match(/[a-zA-Z]/) && value.match(/[0-9]/));
    });

和消息:

    pwd:{
                    required:"Please enter password",
                    minlength:"Please enter minimum characters",
      mypassword: 'Password must contain at least one numeric and one alphabetic character.'

希望这是你所期待的。

【讨论】:

  • 那行得通....小错误,但需要 6 小时才能解决。我必须掌握 jquery..我将从现在开始。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-04-11
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 2014-10-30
  • 2015-12-26
  • 2015-01-31
相关资源
最近更新 更多