Javascript 检查模式是否不匹配

Javascript 检查模式是否不匹配,javascript,regex,validation,regula,Javascript,Regex,Validation,Regula,在中,如何使约束与模式不匹配?我可以这样使用@Pattern: <input type="text" id="categoryId" data-constraints="@Pattern(regex=/[0-9]-[A-Z]{3}-[0-9]{4}/)" /> 我怎样才能在Regula中做到这一点?有几种方法可以做到这一点。对于您的情况,您可以使用负前瞻: <input type="text" id="categoryId" data-constraints="@Patter

在中,如何使约束与模式不匹配?我可以这样使用
@Pattern

<input type="text" id="categoryId" data-constraints="@Pattern(regex=/[0-9]-[A-Z]{3}-[0-9]{4}/)" />

我怎样才能在Regula中做到这一点?

有几种方法可以做到这一点。对于您的情况,您可以使用负前瞻:

<input type="text" id="categoryId" data-constraints="@Pattern(regex=/^(?!.*[0-9]-[A-Z]{3}-[0-9]{4})/)" />
您甚至可以在验证器函数中使用内置的
@模式
验证器,如下所示:

regula.custom({
    name: "NotPattern",
    params: ["regex"],
    defaultMessage: "The value must not match {regex}.",
    validator: function(params, validator) {
        return !validator.pattern(this, params);            
    }
});
然后可以在输入元素中使用它,如下所示:

<input type="text" id="categoryId" data-constraints="@NotPattern(regex=/[0-9]-[A-Z]{3}-[0-9]{4}/)" />
我会把这件事列在我要做的事情清单上

regula.custom({
    name: "NotPattern",
    params: ["regex"],
    defaultMessage: "The value must not match {regex}.",
    validator: function(params, validator) {
        return !validator.pattern(this, params);            
    }
});
<input type="text" id="categoryId" data-constraints="@NotPattern(regex=/[0-9]-[A-Z]{3}-[0-9]{4}/)" />
<input type="text" id="categoryId" data-constraints="@Pattern(regex=/[0-9]-[A-Z]{3}-[0-9]{4}/, invert=true)" />