0
我想讓一個文本框只允許其中最多4個數字,並提出一個警告消息框,如果有任何少於4我已經設法通過使用這個代碼來做到這一點。我想讓一個文本框只能有4個位數在其內
<tr>
<td id="ExamNumber">ExamNumber </td>
<td> <input type="text" name="ExamNumber" maxlength="4"
<font size="1">(Maximum characters: 4)</font> </td>
</tr>
if (document.ExamEntry.ExamNumber.value.length!=4) {
msg+="You must enter at least Four Numbers in the Examination Number \n";
document.ExamEntry.ExamNumber.focus();
document.getElementById('ExamNumber').style.color="red";
result = false;
}
然而,文本框的大小縮水和相比,在代碼中的其他文本框看起來很奇怪,它不是唯一的數字仍然有允許的文本字段中的字母和符號。請提前幫助並感謝您。以下代碼的完整列表如下:
<head>
<title>Exam Entry Form</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 the Exam Number \n";
document.ExamEntry.ExamNumber.focus();
document.getElementById('ExamNumber').style.color="red";
result = false;
}
if (document.ExamEntry.ExamNumber.value.length!=4) {
msg+="You must enter at least Four Numbers in the Examination Number \n";
document.ExamEntry.ExamNumber.focus();
document.getElementById('ExamNumber').style.color="red";
result = false;
}
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">ExamNumber </td>
<td> <input type="text" name="ExamNumber" maxlength="4"
<font size="1">(Maximum characters: 4)</font> </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>
最大長度不允許您鍵入超過400個字符 – 2013-05-09 12:42:48