【问题标题】:Adding a rule for jQuery validation plugin为 jQuery 验证插件添加规则
【发布时间】:2011-03-05 11:37:37
【问题描述】:

我正在使用以下规则使用 jQuery 验证方法来验证用户名。我想添加另一条规则,即用户名应仅包含字母数字和下划线字符。 我如何为该规则添加其他方法。是否有可能如果用户给出的字符少于 4 个,那么我打印最小长度错误消息,如果用户给出无效字符,那么我给出无效字符错误消息?谢谢。

$(document).ready(function() {
$("#sform").validate({
            rules: {
                username: {
                    required: true,
                    minlength: 4
                }
            },

            messages: {
                username: "Minimum length 4.",              
            }
        });
    });

【问题讨论】:

    标签: jquery jquery-validate


    【解决方案1】:

    添加如下

    jQuery.validator.addMethod("alphanumeric", function(value, element) {
        return !jQuery.validator.methods.required(value, element) || /^[a-zA-Z0-9_]+$/i.test(value);
    }
    , "Letters, numbers or underscores only please"); 
    

    并在下方申请

    $('validatorElement').validate({
        rules : {
            username : { alphanumeric : true }
        }
    });
    

    【讨论】:

      【解决方案2】:

      使用远程 ajax 验证执行此操作

      $("#sform").validate({
          rules: {
          username: {
              required: true,
              minlength: 4,
              remote: 'alphanumertic.php'
              //check on server
          }
          },
          messages: {
          username: "Minimum length 4.",              
          }
      });
      

      可能最好的方法是使用正则表达式进行验证,可以在

      上找到

      jQuery validate: How to add a rule for regular expression validation?

      Regular Expression for alphanumeric and underscores

      【讨论】:

        【解决方案3】:

        验证插件附带附加方法.js,其中包括字母数字加下划线验证:

        http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js

        规则同上:

        $('validatorElement').validate({
            rules: {
                username: { alphanumeric : true }
            }
        });
        

        【讨论】:

        • 这要容易得多,而且我认为比使用正则表达式添加新函数更可靠。
        【解决方案4】:

        老问题,但我正在添加我的答案,以便获得帮助
        可以使用以下内容:

        • 创建一个函数来封装我们的验证测试,具有以下签名: function mytest(value, element, params){...}

        • 建立函数后,我们可以将其附加到 jQuery Validation 插件。为此,我们调用验证器对象的 addMethod() 函数。

        我的代码:

        $(document).ready(function() { 
          // other code
          // :  
          // :  
          $.validator.addMethod(
            "passwd",
             function(value, element, regexp) {
                var re = new RegExp(regexp);
                   return this.optional(element) || re.test(value);
             },
            "Invalid input"
           );
        
          $("#add_user_form").validate({
              rules:{
                user_name: {
                   required: true,
                   minlength: 5,
                   maxlength: 15,
                   noSpace: true
                },
                password: {
                  required: true,
                  minlength: 8,
                  passwd: "((?=(.*\\d.*){2,})(?=(.*[a-zA-Z].*){2,})(?=(.*[@#$(){}!~,.!^?/|+=-_%].*){2,}).{8,20})"
              }
            },
        
            messages:{
               user_name: {
                 required: "*",
                 minlength: " at least 5 characters",
                 maxlenght: " only 15 characters",
                 noSpace: " No space Please"
               },
               password: {
                 required: "*",
                 minlength: " Your password must be at least 8 characters long",
                 passwd: " Invalid Password choice"
               }
         }); 
         //
         // more code in document.ready 
        

        好链接:
        Starting With jQuery - How to Write Custom Validation Rules
        How to add a rule for regular expression validation?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-07
          • 1970-01-01
          • 2012-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多