我正在尝试使用jquery.validate来验证jquery模式对话框中的某些字段。练习我想我会创建一个简单的JSFiddle来确保我的语法是正确的。我错过了一些东西,希望有人可以提供帮助。
这是我的简单形式:
<form id="myform" method="post" action="#">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="submit" />
</form>
这是我的jquery:
$("#submitEdit").click(function (event) {
event.preventDefault();
Validate();
});
function Validate()
{
$("#frmEdit").validate({
rules: {
QtyOnHand: {
required: true
}
},
messages: {
QtyOnHand: {
required: 'Qty Required'
}
}
});
}
答案 0 :(得分:1)
<form id="myform" method="post" action="#">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="submit" />
</form>
JS:
$("#myForm").validate({
rules: {
field1: {
required: true
},
field2: {
required: true
}
},
messages: {
field1: {
required: 'field1 Required'
},
field2: {
required: 'field2 Required'
}
}
});
检查 Fiddle
答案 1 :(得分:1)
保持
$("#frmEdit").validate({//this will validate you don't need call validate function
rules: {
QtyOnHand: {
required: true
}
},
messages: {
QtyOnHand: {
required: 'Qty Required'
}
}
});
<小时/> 除去
$("#submitEdit").click(function (event) {
event.preventDefault();//stopping validation to occur
}