jQuery 验证插件使用标题而不是指定的错误消息

发布于 2025-01-01 03:05:41 字数 3163 浏览 1 评论 0原文

当我尝试创建一个方法来使用additional-methods.js 文件中提供的方法时,我得到了预期的验证。但是,如果用户输入错误的值,则用户不会显示在 additional-methods.js 文件中定义的异常,而是会看到当前元素的标题,该标题当前用于 type-暗示。

我的 JavaScript 看起来像这样:

  //setup input validation
                validator = form.validate({
                    rules: {
                        city: {
                            required: true,
                            lettersonly: true
                        }
                    }
                }); 

  $('input, textarea').blur(function(){
                    element = $(this);

                    // if the user gave no value then show the this field is required
                    if(element.val() == element.attr('title')){ 
                        refreshError(element, 'This field is required');
                    }else if(element.val() == '') {  //if the value is empty then check if it was required, also check if it had an input hint
                        //if the element is required display the error message
                        if(element.hasClass('required')){ 
                            refreshError(element, 'This field is required');
                        }

                        //if the title exists then we assume it is the input hint
                        if(element.attr('title') != ''){
                            element.val(element.attr('title'));
                            element.addClass('inputHint');
                        }
                    }else{
                        //if we got input validate it
                        response = element.valid();
                        //if we got an error clear the old one(conditional on existance), and display the new one. Otherwise clear any old error
                        if(!response){
                            this.defaultShowErrors();
                        }else{
                            errorId = element.attr('id')+'Error';
                            $('#'+errorId).remove();
                        }
                    }
                });

表单的相关部分是:

<label for="city">City:</label>
<input type="text" name="city" id="city" class="required inputHint" title="Your city here" />

关于我做错了什么有什么想法吗?我正在使用 1.9.0 版本的 jQuery 验证器插件

注意:读完以下内容后,我想我已经一语中的了:jQuery 验证插件的 addMethod() 显示标题属性的错误 但尝试后:

                    //setup input validation
                validator = form.validate({
                    rules: {
                        city: {
                            required: true,
                            lettersonly: true
                        }
                    },
                    messages: {
                        city: {
                            lettersonly: "you must give a valid city"
                        }
                    }
                }); 

我发现没有任何改变。

When I attempt to create a method to use the methods provided in the additional-methods.js file I get the intended validation. However, if the user inputs a wrong value, instead of displaying the exception defined in the additional-methods.js file, the user sees the title of the current element, which is currently being used for type-hinting.

My JavaScript looks like this:

  //setup input validation
                validator = form.validate({
                    rules: {
                        city: {
                            required: true,
                            lettersonly: true
                        }
                    }
                }); 

  $('input, textarea').blur(function(){
                    element = $(this);

                    // if the user gave no value then show the this field is required
                    if(element.val() == element.attr('title')){ 
                        refreshError(element, 'This field is required');
                    }else if(element.val() == '') {  //if the value is empty then check if it was required, also check if it had an input hint
                        //if the element is required display the error message
                        if(element.hasClass('required')){ 
                            refreshError(element, 'This field is required');
                        }

                        //if the title exists then we assume it is the input hint
                        if(element.attr('title') != ''){
                            element.val(element.attr('title'));
                            element.addClass('inputHint');
                        }
                    }else{
                        //if we got input validate it
                        response = element.valid();
                        //if we got an error clear the old one(conditional on existance), and display the new one. Otherwise clear any old error
                        if(!response){
                            this.defaultShowErrors();
                        }else{
                            errorId = element.attr('id')+'Error';
                            $('#'+errorId).remove();
                        }
                    }
                });

With the relevant part of the form being:

<label for="city">City:</label>
<input type="text" name="city" id="city" class="required inputHint" title="Your city here" />

Any ideas as to what I am doing wrong? I am using the 1.9.0 version of the jQuery Validator plugin

NB: I thought I had hit the nail on the head after reading: addMethod() of jQuery validation plugin display error from title attribute but after trying:

                    //setup input validation
                validator = form.validate({
                    rules: {
                        city: {
                            required: true,
                            lettersonly: true
                        }
                    },
                    messages: {
                        city: {
                            lettersonly: "you must give a valid city"
                        }
                    }
                }); 

I found that nothing changed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2025-01-08 03:05:41

我刚刚遇到此错误并遇到了 ignoreTitle 选项:

//setup input validation
validator = form.validate({
    ignoreTitle: true,
    rules: {
        city: {
            required: true,
            lettersonly: true
        }
    },
    messages: {
        city: {
            lettersonly: "you must give a valid city"
        }
    }
}); 

I just ran into this error and came across the ignoreTitle option:

//setup input validation
validator = form.validate({
    ignoreTitle: true,
    rules: {
        city: {
            required: true,
            lettersonly: true
        }
    },
    messages: {
        city: {
            lettersonly: "you must give a valid city"
        }
    }
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文