У меня небольшие проблемы со следующим. У меня есть несколько полей формы, начиная с «vendorName-» и заканчивая числом. Я хотел бы добавить правило для проверки для этих полей, но я не уверен, как это сделать.
Вот что у меня есть:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="javascript/jquery-1.4.js"></script>
<script type="text/javascript" src="javascript/jquery.validate.js"></script>
<script type="text/javascript" src="javascript/jquery.form.js" ></script>
<script>
$().ready(function() {
$.validator.addMethod(
"findRegex",
function(value, element) {
return /^.+::[1-9]\d{0,11}$/.test(value);
},
"Please check your input."
);
$('#myForm').validate({
submitHandler: function(form){
$("#formSub").html('<table style="font-size:11px; color:#333;"><tr><td><img border="0" src="images/ajax-loader.gif"/></td><td>Saving! Please wait...</td></table>');
var options = {
success: showResponse,
url:'save.cfm'
};
$('#myForm').ajaxSubmit(options);
return false;
}
});
showResponse = function(responseText, statusText) {
$("#formSub").html(responseText);
}
$(":input[name^='vendorName-']").rules("add", { findRegex: true }); // $('input[name^=vendorName]').
});
</script>
</head>
<body>
<form id="myForm" name="myForm" method="post" action="">
<select name="vendorName-1" id="vendorName-1">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
<select name="vendorName-2" id="vendorName-2">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
<select name="vendorName-3" id="vendorName-3">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
<select name="vendorName-4" id="vendorName-4">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
<select name="vendorName-5" id="vendorName-5">
<option value=""></option>
<option value="acme inc::30">acme inc::30</option>
<option value="my company::54781">my company::54781</option>
<option value="abc llc::42443">abc llc::42443</option>
<option value="zzzz">zzzz</option>
</select>
<hr/>
<input type="submit" name="btnSave" id="btnSave" value="Save"/>
<hr/>
<div id="formSub"></div>
</form>
</body>
</html>