[英]clearing a input field if it is invalid
i am using validation plugin. 我正在使用验证插件。 i am trying to figure out how to clear a text field, if the field is invalid .
我试图弄清楚如果该字段无效,如何清除文本字段。
my javascript code is as follows 我的JavaScript代码如下
$(document).ready(function() {
$("#enrollForm").validate({
rules: {
email: {
required: true,
email: true
},
vemail: {
required: true,
email: true,
equalTo: "#email"
},
offer_code: {
required: false,
remote: "check_code.php"
}
}
});});
I am trying to clear out the field called offer_code if the field is invalid. 如果该字段无效,我试图清除名为offer_code的字段。 is it possible to do this with in the validate function?
是否可以在validate函数中做到这一点?
Alex 亚历克斯
Check this out: 看一下这个:
http://docs.jquery.com/Plugins/Validation/valid http://docs.jquery.com/Plugins/Validation/valid
The example: 这个例子:
$(document).ready(function() {
$("#enrollForm").validate({
rules: {
email: {
required: true,
email: true
},
vemail: {
required: true,
email: true,
equalTo: "#email"
},
offer_code: {
required: false,
remote: "check_code.php"
}
}
});});
$("#enrollForm").blur(function() {
if($("#enrollForm").valid()) {
//success code?
} else {
$("#enrollForm").val('')
}
});
I have used jquery to validate a few forms. 我使用jquery来验证一些表单。 For instance, on my site, there is a part where a user has to enter and then re-enter their passwords.
例如,在我的网站上,有一个用户必须输入然后重新输入密码的部分。 If the value of their second password is equal to their first password, I use jquery to slide down a div that says "Your passords match!"
如果他们的第二个密码的值等于他们的第一个密码,我将使用jquery向下滑动显示“您的密码匹配!”的div。
Try this: 尝试这个:
$(document).ready(function(){
$("#error").hide();
$("#invite").hide();
$("#confirm").hide();
$("#invite_short").hide();
$("#short").hide();
$('.button').click(function checkPass(){
var pass = $("#pass").val();
var confirm = $("#con-pass").val();
if(pass != confirm)
{
$("#error").slideDown();
return false;
}
});
$('#con-pass').keyup(function checkPass(){
var pass = $("#pass").val();
var confirm = $("#con-pass").val();
if(pass == confirm)
{
$("#error").slideUp();
$("#confirm").slideDown();
return false;
}
if(pass != confirm)
{
$("#confirm").slideUp();
return false;
}
});
$('#pass').keyup(function checkChar(){
var pass = $("#pass").val().length;
if(pass < 4)
{
$("#short").slideDown();
return false;
}
if(pass == 4
|| pass > 4)
{
$("#short").slideUp();
}
});
$('#invite_number').keyup(function checkCode(){
var code = $("#invite_number").val();
if(code.length == 8)
{
$("#invite_short").slideUp();
$("#invite").slideDown();
return false;
}
if(code.length < 8)
{
$("#invite").slideUp();
return false;
}
});
$('.button').click(function checkCode(){
var code = $("#invite_number").val();
if(code.length < 8)
{
$("#invite_short").slideDown();
return false;
}
});
});
There is a lot of code there. 那里有很多代码。 But, it also validates the form in terms of length requirements.
但是,它还根据长度要求验证了表格。 If the password is less than 4 characters, it notifies them as they're entering their password.
如果密码少于4个字符,它会在输入密码时通知他们。
You get the idea.... Hope this helps 您知道了...。希望这会有所帮助
Assuming that the plugin assigns a label
for the input
with a class of error
, then you could do this: 假设插件为
input
分配了带有error
类的label
,那么您可以这样做:
$('label.error').prev('input').val('');
To be a little more accurate, since the validation plugin also assigns a for
attribute to the error labels: 为了更准确一点,因为验证插件还为错误标签分配了
for
属性:
$(document).ready(
function(){
// call to the plug-in here, input-clearing afterwards
// (though I'm not sure it matters, since it's dependant
// on CSS selectors to work, which aren't dependant on
// the plug-in itself).
$('label.error').each(
function(){
var errorInputID = $(this).attr('for');
$('#'+errorInputID).val('');
});
});
I'm assuming, by 'validation plugin' that you mean this one, at: http://bassistance.de/jquery-plugins/jquery-plugin-validation/ , if I'm wrong then this, obviously, might not work. 我假设,通过'验证插件',你的意思是这个,在: http : //bassistance.de/jquery-plugins/jquery-plugin-validation/ ,如果我错了那么这显然可能不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.