代码之家  ›  专栏  ›  技术社区  ›  Mr world wide

向jQuery添加新的验证方法无效

  •  1
  • Mr world wide  · 技术社区  · 7 年前

    我想验证LinkedIn配置文件URL。这不是必填字段,但当用户输入数据时,它应该只允许正则表达式格式。当不匹配时,应显示消息无效。我试过这样,但它甚至没有触发。我如何调试它?

    jQuery.validator.addMethod("linkedinUrl", function(value, element) {
      return this.optional(element) || /(ftp|http|https):\/\/?(?:www\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(value);
    });
    
    $(function() {
      $("#Add_Staff_Form").validate({
        rules: {
          linkedin {
            required: true;
            linkedinUrl: true;
          }
        },
        messages: {
          linkedin {
            required: "Please specify the correct linkedin url"
          }
        },
        ignore: ':hidden:not([class~=selectized]),:hidden > .selectized, .selectize-control .selectize-input input'
      });
    });
    
    <div class="col-md-4">
      <label class="control-label">Linkedin</label>
      <input type="text" id="linkedin" name="linkedin" class="form-control" value="" />
    </div>   
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   asif    7 年前

    <script>
        jQuery.validator.addMethod("linkedinUrl", function(value, element) {
        return this.optional(element) || /(ftp|http|https):\/\/?(?:www\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(value); }, "Please enter valid linkedin url!" );
    
        $(function () {
            $("#Add_Staff_Form").validate({
                rules: {
                    linkedin:"linkedinUrl",
                },
                ignore: ':hidden:not([class~=selectized]),:hidden > .selectized, .selectize-control .selectize-input input'
            });
        });
    </script>
    

    在html中添加:

    <div class="col-md-4">
        <label  class="control-label">Linkedin</label>
        <input type="text" id="linkedin" name="linkedin" class="form-control linkedinUrl" value=""   />
    </div>