jquery验证和模糊操作

时间:2014-04-04 14:56:35

标签: javascript jquery jquery-validate

我有这个使用jquery验证的应用程序。 (已停止使用数据注释:()

在提交和模糊时,我想检查重复值。由于这是我第一次遇到jquery验证(除了数据注释),我不确定这里最好的方法是什么。这里还有一个远程呼叫。我正在考虑一些像下面这样的事情。这是正确的方法吗?

$("#" + enums.RegisterUsername).rules("add", {
            onkeyup: false,
            **onfocusout/onblur: true, ?????????**
            required: true,
            minlength: 6,
            alphanumericwithbasicpunc: true,
            remote: SignUp.DuplicateUserIdCheckUrl,
            messages: {
                required: "<span style='color:red'>&nbsp;&nbsp;Required</span>",
                minlength: "<span style='color:red'>User name must be at least 6 characters in length.</span>",
                alphanumericwithbasicpunc: "<span style='color:red'>User name cannot contain the following characters: &,\, /, #, <, or >.</span>",
                remote: "<span style='color:red'>User name already taken by another user.</span>"
            }
        });

由于

2 个答案:

答案 0 :(得分:1)

绝对不能.validate()方法中放置任何.rules()方法选项。只有规则(和messages)才能进入。

$("#" + enums.RegisterUsername).rules("add", {
      required: true,
      minlength: 6,
      alphanumericwithbasicpunc: true,
      remote: SignUp.DuplicateUserIdCheckUrl,
      messages: {
          required: "<span style='color:red'>&nbsp;&nbsp;Required</span>",
          minlength: "<span style='color:red'>User name must be at least 6 characters in length.</span>",
          alphanumericwithbasicpunc: "<span style='color:red'>User name cannot contain the following characters: &,\, /, #, <, or >.</span>",
          remote: "<span style='color:red'>User name already taken by another user.</span>"
      }
});

此外,在每种情况下,onfocusout永远不能设置为true。验证模糊已经是默认行为,因此将此选项设置为true可能会破坏插件。如果要禁用它,或者设置为覆盖它的功能,它只能设置为false。如果您希望在模糊时进行验证,则需要将onfocusout选项排除在.validate()之外。

请参阅文档:http://jqueryvalidation.org/validate/

答案 1 :(得分:0)

我评论并测试了以下内容。它正在努力模糊。

$("#" + enums.RegisterUsername).rules("add", {
            //onkeyup: false,
            //onfocusout: true,
            required: true,
            minlength: 6,
            alphanumericwithbasicpunc: true,
            remote: enums.DuplicateUserIdCheckUrl,
            messages: {
                required: "<span style='color:red'>&nbsp;&nbsp;Required</span>",
                minlength: "<span style='color:red'>&nbsp;&nbsp;User name must be at least 6 characters in length.</span>",
                alphanumericwithbasicpunc: "<span style='color:red'>&nbsp;&nbsp;User name cannot contain the following characters: &,\, /, #, <, or >.</span>",
                remote: "<span style='color:red'>&nbsp;&nbsp;User name already taken by another user.</span>"
            }
        });