-1

I am trying to give validation to both fields email and name but only email is getting validated and its not checking for name field. I am not able to find what is wrong in the code.

<html>
<body>
  <form name="myForm">
    Email: <input type="text" name="email" id="email2">
    <br>
    Name: <input type="text" name=" fname">
    <button type="submit" name="submit" onclick="validateForm()">submit</button>
    <br> email
    <div id="email1"></div>
  </form>
</body>
<script>
  function validateForm() {
    var x = document.forms["myForm"]["email"].value;

    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
      alert("Not a valid e-mail address");
      return false;
    }

    var email1 = document.getElementById("email2").value;

    document.getElementById("email1").innerHTML=email1;

    var y = document.forms["myForm"]["fname"].value;
    if (y==null ||y=="") {
      alert("name must be filled");
      return false;    
    }
  }
</script>
</html>
3
  • 3
    That's because if email is invalid return false; will return the control and the code below will not be executed. Commented Jun 16, 2016 at 4:56
  • You can use HTML5 validations <input type="email" name="email" required /> Commented Jun 16, 2016 at 5:03
  • The validation should be on the form's submit handler since the form can be submitted without clicking the submit button. Giving any form control a name of "submit" will make it impossible to call the form's submit method. Commented Jun 16, 2016 at 5:30

3 Answers 3

1
<input type="text" name=" fname">

There is a space before fname.That is why it is not working.You can try simple solution like this

Sign up to request clarification or add additional context in comments.

1 Comment

Name: <input type="text" name="fname" required>
1

You have missed giving id to Name: <input type="text" name=" fname"> Assign ID to Name: <input type="text" name=" fname" id="fname">.

As document.forms["myForm"]["fname"].value; this requires element id to fetch value.

Comments

0

Add an additional boolean to check your validation

function validateForm() {
    var x = document.forms["myForm"]["email"].value;
    //Additional boolean
    var success = true;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        alert("Not a valid e-mail address");
        success = false;
    }
   var email1= document.getElementById("email2").value;
       document.getElementById("email1").innerHTML=email1;

     var y = document.forms["myForm"]["fname"].value;
        if (y==null ||y=="") {
            alert("name must be filled");
            success = false;

    }
    return success;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.