如何擴展javascript代碼,使得考試號只能輸入最大長度4位數字,並且用戶不能再輸入。另外,如何擴展代碼,以便不會遺漏檢查字段。藏漢因爲我想這樣,如果有人檢查任何盒如GCSE一條消息將拿出說你已經選中添加驗證單選按鈕(單選按鈕打勾) 預先感謝HTML/JS Validation
<html>
<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(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="name">Examination number</td>
<td><input type="text" name="Examination number" /></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>
<p> Please select <b>ONE</b> button.</br>
<input type="radio" name="group1" value="GCSE">GCSE<br>
<input type="radio" name="group1" value="AS" checked>AS<br>
<input type="radio" name="group1" value="A2" checked>A2<br>
</body>
</html>
<html>
<head>
<title>Success message</title>
</head>
<body>
<H1> You entered all the data required </H1>
</body>
</html>
相當基本的東西,我會說。一個簡單的Google搜索會爲你提供大量的教程 – asprin
只需在輸入字段中添加maxlength和required,這裏不需要js – EvilEpidemic
'maxlength'很好。但是,如果用戶使用IE9或更低版本呢? – asprin