Javascript表单验证未运行

时间:2014-11-23 23:56:40

标签: javascript

无法获得此表单甚至运行javascript验证。我已经尝试将onclick更改为onsubmit in form但是还没有让它运行脚本。很抱歉,如果这是一个明显的错误,但我已经在互联网上搜索了一段时间。谢谢

<html>
    <head>
        <title>Email Form Page</title>
        <link href="mycss.css" rel="stylesheet">   
        <script type="text/javascript">
            function validator(){
                var firstname = document.getElementsById('fname').value;
                var lastname = document.getElementsById('lname').value;
                var email = document.getElementsById('email').value;
                var subject = document.getElementsById('subject').value;
                var message = document.getElementsById('message').value;
                var errors = "";
                regex1 = /^[a-z]+$/;
                regex2 = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
                // Checks to see if all fields have been filled in
                if (firstname == ""){
                    errors += 'First name field is empty \n';
                   }
                if (lastname == ""){
                    errors += 'Last name field is empty \n';
                   }    
                if (email == ""){
                    errors += 'Your Email field is empty \n';
                   } 
                if (subject == ""){
                    errors += 'Subject field is empty \n';
                   }                    
                if (message == ""){
                    errors += 'Message field is empty \n';
                   }
                // Checks if first name contains only letters     
                if (regex1.text(firstname)) == false {
                    errors += 'First name can not contain numbers \n';
                }
                // Checks if last name contains only leters 
                if (regex1.text(lastname)) == false {
                    errors += 'Last name can not contain numbers \n';
                }   
                // checks to see if email is valid
                if (regex2.text(email) == true) {
                    errors += 'Email was not valid \n';
                }

                //alerts errors and kills form
                if (errors != ""){
                    alert(errors);
                    return false;
                }

        </script> 
    </head>

    <body>
        <h1> Send me an Email </h1>
        <div class= "container">
        <form name="emailform" method="post">
            First Name: <input type='text' name ='firstname' id="fname"> <br>
            Last Name:  <input type='text' name ='lastname' id="lname"> <br>
            Your Email: <input type='text' name ='email' id ="email"> <br>
            Subject:    <input type='text' name='subject' id ="subject">  <br>
            Message:    <textarea rows='6' cols='30' name='message' id="messsage"> </textarea> <br>
            <input type='submit' value ='Send' name='submit' onclick= "validator()">
        </form>
        </div>
    </body>
 </html>

1 个答案:

答案 0 :(得分:0)

您的某些if语句中存在语法错误。

e.g。

 if (regex1.text(firstname)) == false {
                    errors += 'First name can not contain numbers \n';
                }

应该是

if (regex1.text(firstname) == false) {
                    errors += 'First name can not contain numbers \n';
                }

其他一些错误

  • 使用document.getElementsById - 这应该是getElementById,因为它应该只是一个具有此id的元素
  • 消息textarea的ID为'messsage'而不是'message'
  • 帕特里克在评论中说应该有一个测试而不是文本方法。