简体   繁体   English

JavaScript —如何制定规则以确保用户输入的值介于两个特定数字之间

[英]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

You'd use the range option 您将使用range选项

$('#tuition').validate({
    rules: {
        studentID: {
            required: true,
        },
        GPA: {
            range : [0, 4]
        }
    },
    messages: {
        studentID: {
            required: "Please enter your student ID."
        },
        GPA: {
            range: "Please enter a number between 0 and 4."
        }
    }
});

您实际上并不需要Javascript,只需在input代码中使用minmax属性,如下所示:

<input type="number" min="0" max="4" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中让模运算符在两个数字之间添加奇数? - How can I make a modulus operator add odd numbers between two numbers in JavaScript? 如何使它只有数字 0-9 可以输入到 JavaScript 中的提示 window 中? - How do I make it so only numbers 0-9 can be entered into prompt window in JavaScript? 如何确保我的 PHP api 只能由特定的 javascript 页面使用 - How can I make sure that my PHP api can only be used by a specific javascript page 如何使用JavaScript确保每个表行中都有特定的字符串? - How can I make sure a specific string exists in every table row using JavaScript? 如何确保用户在不使用 onSubmit 的情况下从 Select 列表中选择某些内容 - javascript - How can i make sure the user selects something from a Select list without using onSubmit - javascript 如何使这个 javascript function 允许在输入框中输入多个值并用空格分隔? - How can I make this javascript function to allow more than 1 value to be entered into a input box with separated by a space? JavaScript 如何确保下载的文件成功且未损坏 - JavaScript how can I make sure the downloaded file is successful and not corrupted 除了用户输入的值在 1 - 100 之间(例如年龄框),我如何才能制作我的表单 - How can I make my form only except values that the user entered that are between 1 - 100 (e.g like for the age box) 如何确保只有从特定功能重定向的用户才能访问我的网站页面? - How can I make sure that a user visits a page of my site only if he's redirected from a specific function? 如何更改日期输入的 innerHTML 以使该值与当前用户输入的值匹配? - How do I change the innerHTML of a date input to make the value match the currently user-entered value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM