0

I was just wondering if anyone was able to help me with a JavaScript validation routine, Im trying to make it so that the Exam ID Number gets validated after information has been submit, So far all that is needed is name and subject and it will allow the examiner to proceed through without entering the exam ID number, Can anyone help me with this.

<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.subject.value=="") {
            msg+="You must enter your Exam ID Number \n";
            document.ExamEntry.subject.focus();
            document.getElementById('Exam Number').style.color="red";
            result = false;
}


if(msg==""){
return result;
}
{
alert(msg)
return result;
            }


}
</script>
<style type="text/css">
h1,h2,h3,h4,h5,h6 {
            font-family: "Comic Sans MS";
}
</style>

<h1>Exam Entry Form</h1>
  <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="Exam Number">Exam ID Number</td>
                            <td><input type="Number" name="ID Number"maxlength="4" >        </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>
A J
  • 2,112
  • 15
  • 24

1 Answers1

0

Here is the full html

<html>
<head>
    <script type="text/javascript">

        function validateForm() {
            var result = true;
            var msg = "";
            if (document.getElementById("txtname").value == "") {
                msg += "You must enter your name \n";
                document.getElementById("name").focus();
                document.getElementById('name').style.color = "red";
                result = false;
            }

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

            if (document.getElementById("ID_Number").value == "") {
                msg += "You must enter your Exam ID Number \n";
                document.getElementById("ID_Number").focus();
                document.getElementById('Exam Number').style.color = "red";
                result = false;
            }


            if (msg == "") {
                return result;
            }
            else {
                alert(msg)
                return result;
            }


        }
    </script>
</head>
<body>
    <form id="ExamEntry">
    <h1>
        Exam Entry Form</h1>
    <table width="50%" border="0">
        <tr>
            <td id="name">
                Name
            </td>
            <td>
                <input type="text" id="txtname" />
            </td>
        </tr>
        <tr>
            <td id="subject">
                Subject
            </td>
            <td>
                <input type="text" id="txtsubject" />
            </td>
        </tr>
        <tr>
            <td id="Exam Number">
                Exam ID Number
            </td>
            <td>
                <input type="Number" id="ID_Number" maxlength="4">
            </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>
</html>
A J
  • 2,112
  • 15
  • 24