如何在我的表单中使用 jquery 验证器插件中的条件验证
当我从下拉框中选择类型时,表单字段正在发生变化。 更改意味着隐藏或显示,因此根据下拉值表单字段隐藏和显示,我需要向此表单添加j查询验证插件,
我有一个输入
<div class="form-item">
<div class="catergory-inputs">
<input name="address" type="text" class="textarea_medium"
id="Address" value="<?php echo $address['value'] ; ?>" />
</div>
</div>
我验证这个
var x=$("#contactinfo").validate({
rules: {
address: {
required: true,
minlength: 3,
maxlength: 50,
lettersonly: true
},
},
messages: {
address: {
required: "Enter your Address",
minlength: "At least 3 characters long"
},
}
});
我想验证这个字段如果只有这个场上并不隐瞒。像这样
var x=$("#contactinfo").validate({
rules: {
if(not hiding) {
address: {
required: true,
minlength: 3,
maxlength: 50,
lettersonly: true
}
},
},
messages: {
if(not hiding) {
address: {
required: "Enter your Address",
minlength: "At least 3 characters long"
}
},
}
});
我该怎么做,我在堆栈溢出中看到了类似的问题。但是那里没有 mh 问题的答案,请帮助我...................... …… :(
when i select a type from a drop down box the form fields are changing .
changing means hiding or showing , so according to the drop-down value the form fields are hiding and showing , i need to add j query validation plugin to this form ,
i have a input
<div class="form-item">
<div class="catergory-inputs">
<input name="address" type="text" class="textarea_medium"
id="Address" value="<?php echo $address['value'] ; ?>" />
</div>
</div>
i validate this
var x=$("#contactinfo").validate({
rules: {
address: {
required: true,
minlength: 3,
maxlength: 50,
lettersonly: true
},
},
messages: {
address: {
required: "Enter your Address",
minlength: "At least 3 characters long"
},
}
});
i want to validate this field if only this field is not hiding . like this
var x=$("#contactinfo").validate({
rules: {
if(not hiding) {
address: {
required: true,
minlength: 3,
maxlength: 50,
lettersonly: true
}
},
},
messages: {
if(not hiding) {
address: {
required: "Enter your Address",
minlength: "At least 3 characters long"
}
},
}
});
how can i do this , i saw similar questions in stack-overflow .but there were no answer to mh question there , plz help me ............................. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可以使用可见选择器。
http://api.jquery.com/visible-selector/
这是未经测试的,但是;
顺便说一句,当使用验证插件时,你可以这样做;
那么当您调用 validate() 方法时,如果文本框被隐藏,则不会对其进行验证。诀窍是将“必需”类添加到类属性中
I think you can use the visible selector.
http://api.jquery.com/visible-selector/
this is untested but;
By the way, when using the validation plugin you can do this;
then when you call the validate() method, if the textbox is hidden it will not be validated. The trick is to add the "required" class to the class attribute
我想有条件地验证用户名字段,以确保它不在数据库中,而仅在创建新用户时才进行。因此,在创建新用户时,我放置了一个 javascript 变量
var newUser=true;
否则编辑现有用户var newUser=false;
然后在验证的规则部分中放置
required: (newUser)?true:false
这会导致 validate 仅当新用户为
true
时才验证用户名字段。如果我们加载现有用户,则只需在 PHP 代码中添加 javascript var,规则的required
部分就会具有值false
。请记住,您可以将 javascript 放入 json 对象中。因为这都是 javascript 的!
这些是我在学习验证时使用的资源: http://www.ferdychristant.com /blog//articles/DOMM-7LZJN7 //详细介绍如何真正使用 validate 的文章。 http://www.ferdychristant.com/blog//pages/jQuery%20validation %20代码
http://randomactsofcoding.blogspot.com/2008/10/starting-with-jquery-how-to-write.html
http://docs.jquery.com/Plugins/Validation/Methods/required #dependency-expression
http://docs.jquery.com/Plugins/Validation ...您只需点击全部即可!
I wanted to conditionally validate a username field to make sure it was not in the database but only on creating a new user. So when creating a new I placed a javascript variable
var newUser=true;
else editing existing uservar newUser=false;
Then in my rules portion of my validate I put
required: (newUser)?true:false
This causes validate to validate the username field only if new user is
true
. If we loaded up an existing user, then therequired
portion of the rule will have the valuefalse
simply by adding the javascript var in your PHP code.Remember, you can put javascript into a json object. Because it's all javascript!
These are the resources I used in learning about validate: http://www.ferdychristant.com/blog//articles/DOMM-7LZJN7 //article detailing how to really use validate. http://www.ferdychristant.com/blog//pages/jQuery%20validation%20code
http://randomactsofcoding.blogspot.com/2008/10/starting-with-jquery-how-to-write.html
http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression
There is also an amazing depth of information simply in http://docs.jquery.com/Plugins/Validation ...you just gotta click through it all!