验证信息已输入

时间:2014-03-12 19:11:35

标签: javascript

我只是想知道是否有人能够帮助我进行JavaScript验证例程,我试图让它在提交信息后得到验证考试ID号码,到目前为止所需要的只是名称和主题,它将允许审查员在没有输入考试ID号的情况下继续进行,任何人都可以帮助我。

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

1 个答案:

答案 0 :(得分:0)

这是完整的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>