例如,如果用户在上一个文本框中输入5,则该用户只能在下一个输入中输入大于7的数字,因为在上一个输入和下一个输入之间,我需要至少2个数字或更大的数字,否则帮助我。
<!doctype html>
<html>
<head> </head>
<body>
<form action="../" onsubmit="return checknumber(this);">
<input type="number" name="year1" size="20" maxlength="20"> <br> <br>
<input type="number" name="year2" size="20" maxlength="20"> <br>
<input type="SUBMIT" value="Send Address!">
</form>
<script> function checknumber(theForm) {
if (theForm.year1.value > theForm.year2.value) {
alert('Those enter more than 2'); return false;
} else { return true; }
}
</script>
</body>
</html>
答案 0 :(得分:1)
我为您发布了新答案,您可以根据需要进行更新。 您可能还需要参考Javascript tutorial,这可能有助于轻松学习HTML的JavaScript。
尝试以下:
function checknumber() {
var txt1=parseInt(document.getElementsByName('year1')[0].value);
var txt2=parseInt(document.getElementsByName('year2')[0].value);
var lblErr=document.getElementById('lblForYearError');
if(txt2<(txt1+2))
{
lblErr.textContent='Value is less then +2'
}
else if(txt2>(txt1+2))
{
lblErr.textContent='Value is more then +2'
}
else
{
lblErr.textContent='';
}
}
<!doctype html> <html>
<head> </head>
<body>
<form action="../" onsubmit="return checknumber(this);"> <input type="number" name="year1" size="20" maxlength="20"> <br> <br>
<input type="number" name="year2" size="20" maxlength="20" oninput="checknumber(this);"><label id='lblForYearError' style='font-color:red;'></label>
<br>
<input type="SUBMIT" value="Send Address!">
</form> </body>
</html>
答案 1 :(得分:0)
您将要评论自己的答案。
只需要改善您的if
条件并将数据从string
解析为int
,
请从theForm.year1.value > theForm.year2.value
更改if (theForm.year2.value != (theForm.year1.value+2))
:
尝试以下:
function checknumber(theForm) {
if (parseInt(theForm.year2.value) != (parseInt(theForm.year1.value)+2))
{
alert('Those enter more than 2');
return false;
}
return true;
}
<!doctype html> <html>
<head> </head>
<body>
<form action="../" onsubmit="return checknumber(this);"> <input type="number" name="year1" size="20" maxlength="20"> <br> <br>
<input type="number" name="year2" size="20" maxlength="20">
<br>
<input type="SUBMIT" value="Send Address!">
</form> </body>
</html>