使用jQuery validate插件允许在字段上使用破折号

时间:2017-11-14 04:01:06

标签: jquery regex

我尝试为仅接受字母,数字,短划线和下划线的特定字段添加规则,但破折号未包含在规则中。问题是,如何使用以下格式将破折号添加到字符中。

jQuery.validator.addMethod("custom-rule", function(value, element) {
        return this.optional(element) || /^\w+$/i.test(value);
    }, "Letters, numbers, dashes and underscores only please"); 

1 个答案:

答案 0 :(得分:1)

使用character class在正则表达式中添加短划线和单词字符。

const xhr = new XMLHttpRequest();
xhr.onload = function () {
    // TODO: get HTTP response text
};
xhr.open('GET', '/');
xhr.send();