是否有一种简单的方法可以更改jQuery validation plugin中的默认错误值?
我只是想重写错误消息以使我的应用程序更加个性化 - 我有很多字段,所以我不想为字段x单独设置消息...我知道我可以这样做!
答案 0 :(得分:694)
将此代码添加到单独的文件/脚本中 验证插件后覆盖消息,随意编辑:)
jQuery.extend(jQuery.validator.messages, {
required: "This field is required.",
remote: "Please fix this field.",
email: "Please enter a valid email address.",
url: "Please enter a valid URL.",
date: "Please enter a valid date.",
dateISO: "Please enter a valid date (ISO).",
number: "Please enter a valid number.",
digits: "Please enter only digits.",
creditcard: "Please enter a valid credit card number.",
equalTo: "Please enter the same value again.",
accept: "Please enter a value with a valid extension.",
maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
minlength: jQuery.validator.format("Please enter at least {0} characters."),
rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
range: jQuery.validator.format("Please enter a value between {0} and {1}."),
max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});
答案 1 :(得分:217)
您可以在验证呼叫中指定自己的消息。从Validation插件文档(http://jquery.bassistance.de/validate/demo/milk/)中使用的Remember the Milk注册表单中提取和缩写此代码,您可以轻松指定自己的消息:
var validator = $("#signupform").validate({
rules: {
firstname: "required",
lastname: "required",
username: {
required: true,
minlength: 2,
remote: "users.php"
}
},
messages: {
firstname: "Enter your firstname",
lastname: "Enter your lastname",
username: {
required: "Enter a username",
minlength: jQuery.format("Enter at least {0} characters"),
remote: jQuery.format("{0} is already in use")
}
}
}
验证(...)的完整API:http://jqueryvalidation.org/validate
答案 2 :(得分:83)
这对我有用:
$.validator.messages.required = 'required';
答案 3 :(得分:45)
这对我有用:
// Change default JQuery validation Messages.
$("#addnewcadidateform").validate({
rules: {
firstname: "required",
lastname: "required",
email: "required email",
},
messages: {
firstname: "Enter your First Name",
lastname: "Enter your Last Name",
email: {
required: "Enter your Email",
email: "Please enter a valid email address.",
}
}
})
答案 4 :(得分:35)
由于我们已经在使用JQuery,我们可以让页面设计者将自定义消息添加到标记而不是代码:
<input ... data-msg-required="my message" ...
或者,在所有字段上使用单个short data-msg属性的更通用的解决方案:
<form id="form1">
<input type="text" id="firstName" name="firstName"
data-msg="Please enter your first name" />
<br />
<input type="text" id="lastName" name="lastName"
data-msg="Please enter your last name" />
<br />
<input type="submit" />
</form>
然后代码包含这样的内容:
function getMsg(selector) {
return $(selector).attr('data-msg');
}
$('#form1').validate({
// ...
messages: {
firstName: getMsg('#firstName'),
lastName: getMsg('#lastName')
}
// ...
});
答案 5 :(得分:21)
另一种可能的解决方案是遍历字段,向每个字段添加相同的错误消息。
$('.required').each(function(index) {
$(this).rules("add", {
messages: {
required: "Custom error message."
}
});
});
答案 6 :(得分:6)
您可以在下载本地化文件夹中包含一个额外的js文件,而不是更改插件源代码,并在加载validation.js后包含该文件。
jQuery.extend(jQuery.validator.messages, {
required: ...,
maxlength: jQuery.validator.format(...),
...
});
答案 7 :(得分:5)
@Josh:您可以使用资源包中的已翻译消息扩展您的解决方案
<script type="text/javascript">
$.validator.messages.number = '@Html.Raw(@Resources.General.ErrorMessageNotANumber)';
</script>
如果您将此代码部分放入_Layout.cshtml(MVC)中,它可用于您的所有视图
答案 8 :(得分:4)
最新版本有一些不错的在线内容。
如果它是一个简单的输入字段,您可以像这样添加属性data-validation-error-msg
-
data-validation-error-msg="Invalid Regex"
如果你需要更重的东西,你可以使用一个带有传递给validate函数的所有值的变量来完全自定义。请参阅此链接以获取完整详细信息 - https://github.com/victorjonsson/jQuery-Form-Validator#fully-customizable
答案 9 :(得分:3)
我从未想过这会如此简单,我正在研究一个处理此类验证的项目。
以下答案对于想要不费力地更改验证消息的人有很大帮助。
以下方法使用&#34;占位符名称&#34;代替&#34; This Field&#34;。
您可以轻松修改内容
// Jquery Validation
$('.js-validation').each(function(){
//Validation Error Messages
var validationObjectArray = [];
var validationMessages = {};
$(this).find('input,select').each(function(){ // add more type hear
var singleElementMessages = {};
var fieldName = $(this).attr('name');
if(!fieldName){ //field Name is not defined continue ;
return true;
}
// If attr data-error-field-name is given give it a priority , and then to placeholder and lastly a simple text
var fieldPlaceholderName = $(this).data('error-field-name') || $(this).attr('placeholder') || "This Field";
if( $( this ).prop( 'required' )){
singleElementMessages['required'] = $(this).data('error-required-message') || $(this).data('error-message') || fieldPlaceholderName + " is required";
}
if( $( this ).attr( 'type' ) == 'email' ){
singleElementMessages['email'] = $(this).data('error-email-message') || $(this).data('error-message') || "Enter valid email in "+fieldPlaceholderName;
}
validationMessages[fieldName] = singleElementMessages;
});
$(this).validate({
errorClass : "error-message",
errorElement : "div",
messages : validationMessages
});
});
答案 10 :(得分:2)
要删除所有默认错误消息,请使用
$.validator.messages.required = "";
答案 11 :(得分:0)
jQuery表单验证自定义错误消息-tutsmake
$(document).ready(function(){
$("#registration").validate({
// Specify validation rules
rules: {
firstname: "required",
lastname: "required",
email: {
required: true,
email: true
},
phone: {
required: true,
digits: true,
minlength: 10,
maxlength: 10,
},
password: {
required: true,
minlength: 5,
}
},
messages: {
firstname: {
required: "Please enter first name",
},
lastname: {
required: "Please enter last name",
},
phone: {
required: "Please enter phone number",
digits: "Please enter valid phone number",
minlength: "Phone number field accept only 10 digits",
maxlength: "Phone number field accept only 10 digits",
},
email: {
required: "Please enter email address",
email: "Please enter a valid email address.",
},
},
});
});
<!DOCTYPE html>
<html>
<head>
<title>jQuery Form Validation Using validator()</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<style>
.error{
color: red;
}
label,
input,
button {
border: 0;
margin-bottom: 3px;
display: block;
width: 100%;
}
.common_box_body {
padding: 15px;
border: 12px solid #28BAA2;
border-color: #28BAA2;
border-radius: 15px;
margin-top: 10px;
background: #d4edda;
}
</style>
</head>
<body>
<div class="common_box_body test">
<h2>Registration</h2>
<form action="#" name="registration" id="registration">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" placeholder="John"><br>
<label for="lastname">Last Name</label>
<input type="text" name="lastname" id="lastname" placeholder="Doe"><br>
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone" placeholder="8889988899"><br>
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="john@doe.com"><br>
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder=""><br>
<input name="submit" type="submit" id="submit" class="submit" value="Submit">
</form>
</div>
</body>
</html>
答案 12 :(得分:0)
"mongoose": "^5.11.9",
"@types/mongoose": "^5.10.3",
"typescript": "^3.7.2"
答案 13 :(得分:-1)
添加自定义或字段标签或与正在验证的字段相关的任何动态。 您可以通过 dom 选择任何内容并将其添加到任何带有错误消息的位置。
以下是在必填消息的开头添加字段标签而不是“此字段”的示例。
greet {
message = 'Hi from Gradle'
}
答案 14 :(得分:-2)
我们可以指定文本字段的类型,而不是这些自定义错误消息。
例如:将字段的类型设置为type ='email'
然后插件将识别该字段并正确验证。