[英]JavaScript — How can I make a rule to make sure a user entered a value between two specific numbers
How can I make sure a user entered a value between two numbers on a form? 如何确保用户在表单上的两个数字之间输入了一个值? In this instance, one of the boxes asks the user to enter their GPA.
在这种情况下,其中一个框会要求用户输入其GPA。 I need to ensure that the number is between 0 and 4. They cannot click the submit button until the number is between 0 and 4.
我需要确保该数字介于0和4之间。只有在数字介于0和4之间时,他们才能单击“提交”按钮。
Can I do something like: 我可以做类似的事情吗?
GPA: {
min: 0,
max: 4
}
If you look at the code below I already have one rule (Student ID input box is required)... How can I incorporate the GPA rule in there? 如果您看下面的代码,我已经有一个规则(需要输入学生ID)...如何在其中合并GPA规则?
$(document).ready(function(){
$('#studentID').focus();
//validate form
$('#tuition').validate({
rules: {
studentID: {
required: true,
}
},//end rules
messages: {
studentID: {
required: "Please enter your student ID.",
}
},//end messages
});//end validate
//School auto-complete
您实际上并不需要Javascript,只需在input
代码中使用min
和max
属性,如下所示:
<input type="number" min="0" max="4" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.