How to set a min and max character length on a HTM

2019-06-11 15:52发布

I am only GCSE level and I need a text box in my code to only have 4 characters. I have made it a max of 4 but I need it to have a minimum of 4 aswell. I also made it only accept numbers. I am not using HTML 5 at my school.

This is my whole code:

 <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>

1条回答
forever°为你锁心
2楼-- · 2019-06-11 16:33

Well you can't have a minlength because your users are going to need to type 1,2 and 3 characters to get to 4. (Unless you apply some sort of input mask which is more advanced). But you can check that it is 4 characters long when it is blurred..

<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" />

And then a simple JavaScript function..

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.");
    }
}
查看更多
登录 后发表回答