0

我只有 GCSE 级别,我的代码中需要一个文本框,只有 4 个字符。我已将其设置为最多 4 个,但我也需要它至少有 4 个。我也让它只接受数字。我没有在我的学校使用 HTML 5。

这是我的整个代码:

 <head>
    <title>Exam entry</title>
    <script language="javascript" type="text/javascript">
    function validateForm() {
    var result = true;
    var msg="";




if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject. \n";
document.ExamEntry.subject.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.ExamNumber.value=="") {
msg+="You must enter your exam number. \n";
document.ExamEntry.ExamNumber.focus();
document.getElementById('ExamNumber').style.color="red";
result = false;
}

if(document.ExamEntry.ExamNoLen.value.length <4) { 
msg+="Your eaxm nuber must be 4 digits long.  \n";
document.ExamEntry.ExamNumber.focus();
document.getElementById('ExamNumber').style.color="red";
result = false;
}

checkInput(elem){
    if(elem.value.length != 4){
        alert("This value needs to be 4 characters long!");
        elem.value = ""; // Reset the textbox
    }
    else{
        alert("This value is 4 characters long.");
    }
}

if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}
</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
  <td id="subject">Subject</td>
  <td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="ExamNumber">Exam Number </td>
<td>
  <input name="ExamNumber"  type="text"  
  onkeydown="return ( event.ctrlKey || event.altKey 
                    || (47<event.keyCode && event.keyCode<58 && event.shiftKey==false) 
                    || (95<event.keyCode && event.keyCode<106)
                    || (event.keyCode==8) || (event.keyCode==9) 
                    || (event.keyCode>34 && event.keyCode<40) 
                    || (event.keyCode==46) )" 
                    maxlength="4" onblur="checkInput(this)"  />
    </td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onClick="return validateForm();" /></td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>
4

1 回答 1

1

好吧,您不能有 minlength,因为您的用户需要输入 1,2 和 3 个字符才能达到 4。(除非您应用某种更高级的输入掩码)。但是你可以检查它模糊时是 4 个字符长..

<input name="ExamNumber" type="text" onblur="checkInput(this)" onkeydown="return ( event.ctrlKey || event.altKey || (47<event.keyCode && event.keyCode<58 && event.shiftKey==false) || (95<event.keyCode && event.keyCode<106) || (event.keyCode==8) || (event.keyCode==9) || (event.keyCode>34 && event.keyCode<40) || (event.keyCode==46) )" maxlength="4" />

然后是一个简单的 JavaScript 函数..

function checkInput(elem){
    if(elem.value.length != 4){
        alert("This value needs to be 4 characters long!");
        elem.value = ""; // Reset the textbox
    }
    else{
        alert("This value is 4 characters long.");
    }
}
于 2013-01-21T14:48:14.007 回答