1

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

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

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

这是我的 HTML 代码片段

  <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 中未定义的方法“调用”

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

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

4

1 回答 1

0

Please check the example in the fiddler link : http://jsfiddle.net/NaaEw/

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

and message :

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

Hope this is what you are expecting.

于 2014-03-21T12:10:51.497 回答