0

My javascript form validation is working correctly. I want it so that when the form is valid, it will go to a different page. I am having trouble with that part. I tried using the document object to submit it if everything is valid but its not working

Javascript:

function func(){
    var first = document.getElementById('fname').value;
    var last = document.getElementById('lname').value;
    var email = document.getElementById('mail').value;
    var phone = document.getElementById('phone').value;
    var val_phone = /^\(\d{3}\)\d{3}-\d{4}$/;
    var val_mail = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;

    if ( first == "" || last == "" || email == "" || phone == "")
    {
        alert("Do not Leave Any Blank Answers");
        return;
    }           
    if ( phone != phone.match(val_phone) || email != email.match(val_mail)  )
    {
        alert("Incorrect Format! \n Please Check Email and Phone Number! ");
        return;                         
    }
    else  {
        document.forms["survey"].sumbit();              
    }
}

HTML:

<form id="survey" name="survey" action="SlideShow.html" method="post">
    First Name:<br>
    <input type="text" id="fname" name="fname" required="required"><br>
    Last Name:<br>
    <input type="text" id="lname" name="lname" required="required"><br>
    Email:<br>
    <input type="email" id="mail" name="mail" required="required"><br>
    Phone Number:<br>
    <input type="text" id="phone" name="phone" required="required"><br><br>             
    <input type="button" value="Submit" onclick="func()">
</form>
Samurai
  • 3,724
  • 5
  • 27
  • 39
Timothy Wong
  • 95
  • 1
  • 1
  • 14
  • 1
    That would be `submit()`, rather than `sumbit()` :) That being said, you should do the inverse and do a `preventDefault` only if there is an error (rather than having the prevention by default). – Jesse Kernaghan May 29 '15 at 23:38
  • Yep error on that line: http://jsfiddle.net/b2us3uwd/2/ – garryp May 29 '15 at 23:43

1 Answers1

0

Your else block is calling sumbit(), but the proper spelling is submit().

Additionally, I recommend getting in the habit of a strict === check as opposed to a ==.

Here's a JSFiddle with the updated and refactored code:

http://jsfiddle.net/cyeof94g/

Community
  • 1
  • 1
Richard Kho
  • 5,086
  • 4
  • 21
  • 35