我有一个年龄的文本框。提交表单时,我想从客户端检查文本框中的值是否大于 18,如果年龄不大于 18,则显示消息。
我现在遇到的问题是,我的自定义验证器在我输入文本时显示消息,而当我输入低于 18 的数字时根本不显示。感谢任何帮助。谢谢你。
这是我的自定义验证器:
<script>
$(document).ready(function(){
$.validator.addMethod('ageGreaterEighteen', function(value){
return parseFloat(value) > 0;
}, 'Age has to be greater than 18');
$.validator.addMethod("textOnly", function(value) {
return !/[0-9]+/.test(value);
}, "Alpha Characters Only.");
$("#TestForm").validate({
rules: {
txtName: {
required: true,
textOnly: true
},
txtAge: {
required: true,
ageGreaterEighteen: true
}
},
messages: {
txtName: {
required: "* Required",
textOnly: "Please enter text only"
},
txtAge: {
required: "* Required",
ageGreaterEighteen: "Age has to be greater than 18!"
}
}
});
})
</script>
这是我的表格:
<form id="TestForm">
<label for="txtName">Name: </label><br />
<input name="txtName" type="text" id="txtFirstName" /><br />
<label for="txtAge">Age: </label><br />
<input name="txtAge" type="text" id="txtAge" /><br />
<input type="submit" value="Submit" id="btnSubmit" />
</form>