[英]How can I make the custom css code for invalid values while filling the form in html?
I'm using the plugin from http://jqueryvalidation.org/ to validate my form. 我正在使用http://jqueryvalidation.org/中的插件来验证我的表单。 The html code is simple:
html代码很简单:
<form id="myform">
<input type="text" name="field1" /> <br/>
<textarea maxlength="140" style="resize:none" class="textoxarea" placeholder="zbzbzxcbcx" name="field2"></textarea>
<input type="checkbox" name="terms" />x
<input type="submit" />
</form>
and the javascript is: 和JavaScript是:
$(document).ready(function() {
$('#myform').validate({ // initialize the plugin
errorElement: 'div',
rules: {
field1: {
required: true,
email: true
},
field2: {
required: true,
minlength: 5
},
terms: {
required: true,
maxlength: 2
}
},
submitHandler: function(form) { // for demo
alert('valid form submitted'); // for demo
return false; // for demo
}
});
});
It works, as you can see in my fiddle , but I would like to achieve slightly different effect. 正如您在我的小提琴中看到的那样,它可以正常工作,但是我想获得稍微不同的效果。 I would like to - instead of showing the error message below the fields, just change their css style, so that when the value doesn't meet its requirements - each component will get the small red border (1px).
我想-而不是在字段下方显示错误消息,只需更改其CSS样式,以便当该值不符合其要求时-每个组件都会获得小的红色边框(1像素)。 Btw, is there a way to make the button active only when the three options above are filled properly?
顺便说一句,有没有一种方法可以使按钮只有在正确填写以上三个选项后才能激活? Thanks!
谢谢!
Simply use the CSS: 只需使用CSS:
.textox.error, .textoxarea.error,
.textox.error:focus, .textoxarea.error:focus{
border:1px solid red; /* add the border to your input */
}
.error[generated=true]{
display:none!important; /* hide the error message, overriding the inline styling */
}
To make the button active only when the form is valid, disable it by default: 要使按钮仅在表单有效时才激活,默认情况下禁用它:
<input type="submit" disabled />
Then, check validation of the form when an input value changes, and enable if valid: 然后,在输入值更改时检查表单的有效性,并在有效时启用:
$('#myform').find('input, textarea').on('change', function () {
var btn = $('#myform').find('input[type=submit]');
if ($('#myform').valid()) {
btn.removeAttr('disabled');
} else {
btn.attr('disabled', 'disabled');
}
});
Add the error placement handler in the validate function like below- 将错误放置处理程序添加到validate函数中,如下所示:
errorPlacement: function (error, element) {
}
You can handle how and where the error text label should be displayed or not be displayed at all, if you don't specify in this function callback. 如果未在此函数回调中指定,则可以处理应该显示或完全不显示错误文本标签的方式和位置。 In this way the label text will not exist in the DOM.
这样,标签文本将不会在DOM中存在。
And then add the css for the "error" class which is added to the control by the validator plugin like this- 然后为“错误”类添加css,该类由验证器插件添加到控件中,如下所示:
.error{
border: 1px solid red !important;
}
Update: 更新:
The complete code for the cleaner way to handle your requirements- 完整的代码,可以更清晰地处理您的要求-
$(document).ready(function () {
$('input[type="submit"]').attr('disabled', 'disabled');
var validator = $('#myform ').validate({ // initialize the plugin
rules: {
email: {
required: true,
email: true
},
field2: {
required: true,
minlength: 5
},
terms: {
required: true,
maxlength: 2
}
},
errorPlacement: function (error, element) {},
success: function () {
if(validator.numberOfInvalids() == 0)
{
$('input[type="submit"]').removeAttr('disabled');
}
else
{
$('input[type="submit"]').attr('disabled', 'disabled');
}
},
submitHandler: function (form) { // for demo
alert('valid form submitted '); // for demo
return false; // for demo
}
});
validator.form();
$('input[type="checkbox"]').on('click',function(){
validator.form();
});
});
Fiddle Url- http://jsfiddle.net/xvAPY/228/ 小提琴网址-http: //jsfiddle.net/xvAPY/228/
Sounds like you want the highlight option? 听起来像您想要突出显示选项?
$(".selector").validate({
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).removeClass(validClass);
$(element.form).find("label[for=" + element.id + "]")
.addClass(errorClass);
},
unhighlight: function(element, errorClass, validClass) {
$(element).removeClass(errorClass).addClass(validClass);
$(element.form).find("label[for=" + element.id + "]")
.removeClass(errorClass);
}
});
Then add .errorClass to your CSS and define what you want it to look like. 然后将.errorClass添加到CSS并定义您想要的外观。
All options can be found here: http://jqueryvalidation.org/validate 所有选项都可以在这里找到: http : //jqueryvalidation.org/validate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.