简体   繁体   English

无法在jQuery验证引擎中指定自定义错误

[英]Unable to specify custom error in jQuery validation engine

I am fiddling with the jQuery validation engine documented here 我正在摆弄这里记录的jQuery验证引擎

I have downloaded all JS files locally. 我已在本地下载了所有JS文件。 And created a simple form with single required textbox field. 并创建了一个包含单个必需文本框字段的简单表单。

Problem 1 问题1

I am unable to specify custom error for this required rule (even though I have specified custom-error-message option). 我无法为此必需规则指定自定义错误(即使我已指定custom-error-message选项)。 Instead it shows the default message "This field is required." 相反,它显示默认消息“此字段是必需的”。 I have written this code with the help of example (shown here ) on the git, but it is not working. 我写了这个代码示例(所示的帮助下在这里 )的git的,但它不工作。 Please point out where it is going wrong. 请指出哪里出错了。 (I want to do it with JS object literals instead of using equivalent html5 attributes.) (我想用JS对象文字来代替使用等效的html5属性。)

My code: 我的代码:

<html>
<head>      
    <script type="text/javascript" src="scripts/jquery-1.9.0.min.js"></script>      
    <script type="text/javascript" src="scripts/jquery.validationEngine-en.js"></script>        
    <script type="text/javascript" src="scripts/jquery.validationEngine.js"> </script>      
    <link type="text/css" rel="stylesheet"  href="styles/validationEngine.jquery.css"  />   
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    <script type="text/javascript">
            $(document).ready(function(){
                $("#form1").validationEngine({'custom_error_messages' : {                                
                            '#username':{
                                'required':{
                                    'message':"Hey you cannot leave this field blank."
                                }
                            }     
                        }
                    });                         
                });

    </script>
</head>
<body>
    <form id="form1">
        <br /><br />
        <h2>Form 1</h2>
        Username: <input id="#username" type="text" class="validate[required]" ></input> <br />     
    </form>
</body>
</html>

Problem 2 问题2

How can I specify what rule to apply to which element in JS instead of specifying class values in html. 如何指定应用于JS中哪个元素的规则,而不是在html中指定类值。 I will prefer to have clean unobtrusive html instead of mixing validation specific stuff in html. 我宁愿使用干净的不显眼的HTML,而不是在HTML中混合验证特定的东西。 This is possible with jQuery Validation plugin as follows: 这可以通过jQuery Validation插件实现 ,如下所示:

$("#register-form").validate({
                rules: {
                    firstname: "required",
                    lastname: "required",
                    email: {
                        required: true,
                        email: true
                    },
                    password: {
                        required: true,
                        minlength: 5
                    },
                    agree: "required"
                },
                messages: {
                    firstname: "Please enter your firstname",
                    lastname: "Please enter your lastname",
                    password: {
                        required: "Please provide a password",
                        minlength: "Your password must be at least 5 characters long"
                    },
                    email: "Please enter a valid email address",
                    agree: "Please accept our policy"
                },
                submitHandler: function(form) {
                    form.submit();
                }
            });

Is this possible with the jQuery Validation engine. 这是否可以使用jQuery Validation引擎。

yes it is possible : link 是的,有可能: 链接

exemple : 例如:

<input type="email" name="email" id="email" data-validation-engine="validate[required,custom[email]]"
    data-errormessage-value-missing="Email is required!" 
    data-errormessage-custom-error="Let me give you a hint: someone@nowhere.com" 
    data-errormessage="This is the fall-back error message."/>

Also please note that since JQuery 1.9 many functions that worked in past version are now removed or deprecated and a fix was added tree days ago, so you might want to download the latest version : jQuery.validationEngine v2.6.1 另请注意,由于JQuery 1.9现在删除或弃用了许多在过去版本中运行的函数,并且在几天前添加了修复程序,因此您可能需要下载最新版本: jQuery.validationEngine v2.6.1

edit don' t know if they corrected it yet but in the .js file replace 编辑不知道他们是否已更正,但在.js文件中替换

line28 : .live(...

by 通过

line 28 : .on(...

live has been removed since jQuery 1.9 live已从jQuery 1.9中删除

edit: 编辑:

you did a small mistake : 你犯了一个小错误:

<input id="**#**username" type="text" class="validate[required]" ></input> <br />

the # should be there : 应该在那里:

<input id="username" type="text" class="validate[required]" ></input> <br/>  

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

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