我正在尝试使用jQuery Validate Plugin验证我的表单。我的表单至少需要一对这样的输入(例如:mobilePhone
/mobilePhone_p
或/和personalPhone
/personalPhone_p
或/和workPhone
/ workPhone_p
),但我不知道如何将一个文本字段(电话号码)与他的相关单选按钮(首选是/否)链接用于验证我的表格并验证至少有一对。如果设置了一对或多对,我的表单还需要至少一个选中的单选按钮。
HTML 表单:
<form method="post" action="#" id="phoneForm">
<table>
<tr>
<td><label for="mobilePhone">Mobile</label></td>
<td>
<input type="text" class="mp_phone" name="mobilePhone" id="mobilePhone" value="" />
</td>
<td><label for="mobilePhone_p">Preferred phone number</label> <input type="radio" name="num_p" id="mobilePhone_p" value="mobilePhone_p" /></td>
</tr>
<tr>
<td><label for="personalPhone">Personal</label></td>
<td>
<input type="text" class="mp_phone" name="personalPhone" id="personalPhone" value="" />
</td>
<td><label for="personalPhone_p">Preferred phone number</label> <input type="radio" name="num_p" id="personalPhone_p" value="personalPhone_p" /></td>
</tr>
<tr>
<td><label for="workPhone">Work</label></td>
<td>
<input type="text" class="mp_phone" name="workPhone" id="workPhone" value="" />
</td>
<td><label for="workPhone_p">Preferred phone number</label> <input type="radio" name="num_p" id="workPhone_p" value="workPhone_p" /></td>
</tr>
</table>
<button type="submit" id="validateFormButton">Submit</button>
</form>
jQuery 验证:
$("#phoneForm").validate({
rules: {
mobilePhone: {
phone: true,
require_from_group: [1,".mp_phone"]
},
personalPhone: {
phone: true,
require_from_group: [1,".mp_phone"]
},
workPhone: {
phone: true,
require_from_group: [1,".mp_phone"]
},
num_p: {
required: true
}
}
});
(phone
是一种验证电话号码的自定义方法,require_from_group
在http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js中定义)
当字段需要至少一个文本字段但require_from_group
在其他字段上设置单选按钮组时未验证单选按钮组时,它可以工作......
如何使用 jQuery Validation 插件验证我的表单?