简体   繁体   English

jQuery:尝试向UF Validator添加自定义验证

[英]JQuery: Trying to add custom validation to UF Validator

I am using the UF Validator on a project and I'd like to add some custom validation checks. 我正在一个项目上使用UF验证程序 ,我想添加一些自定义验证检查。

I want to add a username check that gives an error if the input contains anything other than az 0-9. 我想添加一个用户名检查,如果输入中包含az 0-9以外的任何内容,则会给出错误提示。 No spaces or anything. 没有空格或任何东西。 Similarly I want to add a name check that only allows az, but does allow spaces. 同样,我想添加一个名称检查,该检查只允许az,但是允许空格。

I am basing my checks on the built in email validation. 我的支票基于内置的电子邮件验证。 Here is the filter: 这是过滤器:

var mail_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

And here is the code for the email check: 这是电子邮件检查的代码:

// E-MAIL VALIDATION
    if (obj.hasClass('req-email')) {
        tmpresult = mail_filter.test(valAttr);
        if (!tmpresult) errorTxt = (valAttr == '') ? opts.errorMsg.reqMailEmpty : opts.errorMsg.reqMailNotValid;
        result = result && tmpresult;
    }

And here are my filters: 这是我的过滤器:

var username_filter = /^[a-zA-Z0-9]/;
var name_filter = /^[a-zA-Z ]/;

And here is my code: 这是我的代码:

// USERNAME VALIDATION
    if (obj.hasClass('req-username')) {
        tmpresult = username_filter.test(valAttr);
        if (!tmpresult) errorTxt = (valAttr == '') ? opts.errorMsg.reqUsernameEmpty : opts.errorMsg.reqUsernameNotValid;
        result = result && tmpresult;
    }
// NAME VALIDATION
    if (obj.hasClass('req-name')) {
        tmpresult = name_filter.test(valAttr);
        if (!tmpresult) errorTxt = (valAttr == '') ? opts.errorMsg.reqNameEmpty : opts.errorMsg.reqNameNotValid;
        result = result && tmpresult;
    }

I have referenced them here as well: 我在这里也引用了它们:

// gather inputs & check is valid
    $(merged_options.scope+' .req-email, '+merged_options.scope+' .req-string, '+merged_options.scope+' .req-same, '+merged_options.scope+' .req-both, '+merged_options.scope+' .req-numeric, '+merged_options.scope+' .req-date, '+merged_options.scope+' .req-min, '+merged_options.scope+' .req-username, '+merged_options.scope+' .req-name').each(function() {
        thisValid = $.formValidator.validate($(this),merged_options);
        boolValid = boolValid && thisValid.error;
        if (!thisValid.error) errorMsg  = thisValid.message;
    });

The corresponding error messages exist as well but I don't think they need showing. 相应的错误消息也存在,但我认为它们不需要显示。

I thought this would work fine but when I try, it behaves quite strangely. 我以为这会很好,但是当我尝试时,它的表现却很奇怪。 Showing the wrong error messages and not validating it how I want it to. 显示错误的错误消息,并且没有按照我的意愿对其进行验证。 I've checked their website and searched on here but can't find anything relating to it. 我已经检查了他们的网站并在这里搜索,但是找不到任何与此相关的信息。

Your regex only test for the presence of a single character at the start of the string. 您的正则表达式仅测试字符串开头是否存在单个字符。
You need to add a + to indicate that you want to match one or more characters, eg [az\\d]+ , or you can specify a range. 您需要添加一个+来表示您要匹配一个或多个字符,例如[az\\d]+ ,或者您可以指定一个范围。

For example, the regex below specify that the strings must be between 1 and 20 characters long. 例如,下面的正则表达式指定字符串必须在1到20个字符之间。

var username_filter = /^[a-z\d]{1,20}$/i;
var name_filter = /^[a-z ]{1,20}$/i; 

The end character $ is needed, or you are only specifying what must be at the start of the string. 需要结尾字符$ ,或者您仅指定字符串开头必须包含的字符。

The i flag means a case-insensitive match, assuming that is what you want; i标志表示不区分大小写的匹配,假设这就是您想要的; \\d is equivalent to [0-9] . \\d等效于[0-9]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM