RegEx在jQuery Validator addMethod中 [英] RegEx in jQuery Validator addMethod

查看:73
本文介绍了RegEx在jQuery Validator addMethod中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jQuery验证器自定义方法需要一些RegEx。我认为第一个容易的只是将用户名限制为只有美国字母,数字和没有特殊字符。我想出了 ^ [a-zA-Z0-9] + $ 但它似乎不起作用。也许是因为它出现在函数中的方式:

I need a few RegEx for my jQuery validator custom methods. The first which I thought was going to be easy was just limiting a username to only US letters, numbers, and no special characters. I came up with ^[a-zA-Z0-9]+$ but it doesn't seem to work. Maybe it is due to the way it appears in the function:

$.validator.addMethod(
    "legalName",
    function(value, element) {
        return (element.value != "^[a-zA-Z0-9]+$");
    },
    "Use a valid username."
);

第二个是验证密码,这个限制与增加所需的确定数量基本相同字符,如大写,数字等。我想,一旦我得到用户名RegEx,密码就不会太难,只需要在 {1} 中填写表达式。

The second is validating the password which is essentially the same limitations with the addition of a required number of certain characters such as an uppercase, number, ect. I figure once I get the username RegEx down the password wont be too hard, just a matter of fitting in {1}'s and such within the expression.

我的问题的最后一部分是如何在原始 .addMethod 中添加第二种方法(legalPassword)或我需要创建一个完整的其他 $。validator.addMethod

The last part of my question is how do I add a second method ("legalPassword") inside of the original .addMethod or do I need a create a whole other $.validator.addMethod?

$(document).ready(function() {
    $("#form1").validate({
        rules: {
            username: {
                legalName: true,
                required: true,
                maxlength: 35
            }
        },
    });
});

和表格代码:

<form id="form1" method="post" action="">
  <div class="form-row"><span class="label">Username *</span><input type="text" name="username" /></div>
  <div class="form-row"><input class="submit" type="submit" value="Submit"></div>
</form>

新代码(仍无效):

$.validator.addMethod(
    "legalName",
    function(value, element) {
        /^[a-zA-Z0-9]+$/.test( value );
    },
    "Use a valid username."
);


推荐答案

返回this.optional(元素)|| / ^ [a-zA-Z0-9] + $ / .test(value);

这篇关于RegEx在jQuery Validator addMethod中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆