I have been looking through my GCSE project at school and I was trying to make the project look better by making a particular section of my code only accept numbers.
I have made it so that this section of my code limits the user from entering more than 4 numbers . is there a way to make the code only accept numbers ?
<td>
<input type="text" name="number" maxlength="4" />
</td>
</tr>
- full code snippet
<head>
<title>Exam entry</title>
<script language="javascript" type="text/javascript">
function validateForm() {
var result = true;
var msg="";
if (document.ExamEntry.name.value=="") {
msg+="Please enter your name \n";
document.ExamEntry.name.focus();
document.getElementById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="Please enter your subject \n";
document.ExamEntry.name.focus();
document.getElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.number.value == ""){
msg+="Please enter your examination number \n";
document.ExamEntry.number.focus();
document.getElementById('number').style.color="red";
result = false;
} else if ( (document.ExamEntry.number.value > 999) & (document.ExamEntry.number.value < 10000)) {
var ExamNumber = confirm("Are you sure its the correct examination number ? "+document.ExamEntry.number.value);
if(ExamNumber == true) {
} else {
alert("Oops . You need to enter your examination number correctly to continue");
result = false;}
} else {
msg+=" Oops . You forgot to enter the correct examination number \n";
result = false;
}
var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
checked = inputs[i];
}
}
if(checked==null)
{
msg += "Please select your level of entry \n";
result = false;
}
else{
return confirm('You have decided to choose '+checked.value+' as your level of entry for the exam . Is this correct? If so good luck .');
}
if(msg === ""){
return result;
} else {
alert(msg);
return result; }
}
</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html" onSubmit="return validateForm();">
<table width="50%" border="0">
<tr>
<td id="name">Name</td>
<td><input type="text" name="name" /></td>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<td id="number">Examination Number</td>
<td><input type="text" name="number" maxlength="4"/></td>
</tr>
<tr>
<p><b>Note:</b> Please don't forget to click on the type of exam that you are sitting.</p>
<td><input type="radio" id="examtype" name="examtype" value="GCSE" /> GCSE<br />
<td><input type="radio" id="examtype" name="examtype" value="A-levels" /> A-levels<br />
<td><input type="radio" id="examtype" name="examtype" value="Betec"/> Betec<br />
<td><input type="radio" id="examtype" name="examtype" value="Other"/> Other<br />
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" /> </td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
</table>
</form>
</body>