0

I am validating my form using javascript validation as whole validation is working fine but email validation is not working. My form code is as follows

<form method="post" action="contact.php" name="myForm" onsubmit="return validateForm()">

<h3>To know more contact us today.</h3>

    <table>
        <tr>
            <td>Name:
                <br />
                <input id="name" name="name" type="text" />
            </td>
        </tr>
        <tr>
            <td>Contact No:
                <br />
                <input id="contact" name="contact" type="text" />
            </td>
        </tr>
        <tr>
            <td>Email:
                <br />
                <input id="email" type="text" name="email" />
            </td>
        </tr>
        <tr>
            <td>Address:
                <br />
                <textarea cols="34" id="address" name="address" type="text"></textarea>
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>
</form>

and my javascript code is as follows

  <script type="text/javascript">
        function validateEmail() {

            var emailID = document.["myForm"]["email"].value;
            atpos = emailID.indexOf("@");
            dotpos = emailID.lastIndexOf(".");
            if (atpos < 1 || (dotpos - atpos < 2)) {
                alert("Please enter correct email ID")
                document.myForm.email.focus();
                return false;
            }
            return (true);
        }
        function validateForm() {
            var x = document.forms["myForm"]["name"].value;
            if (x == null || x == "") {
                alert("First name must be filled");
                return false;
            }
            var x = document.forms["myForm"]["contact"].value;
            if (x == null || x == "" || isNaN(document.myForm.contact.value) || document.myForm.contact.value.length != 10) {
                alert("Contact Number Must be 10 Digits");
                return false;
            }
            var x = document.forms["myForm"]["email"].value;
            if (x == null || x == "") {
                alert("Email is must");
                return false;
            }
            else {

                var ret = validateEmail();
                if (ret == false) {
                    return false;
                }
            }
            var x = document.forms["myForm"]["address"].value;
            if (x == null || x == "") {
                alert("Address cannot be empty");
                return false;
            }
            return (true);
        }

    </script>
Raekye
  • 5,081
  • 8
  • 49
  • 74
عثمان غني
  • 2,786
  • 4
  • 52
  • 79
  • 1
    first validation is logic bad. have you heard of regEx ? – Ravi Gadag Jan 21 '13 at 06:53
  • And what error do you get – Hanky Panky Jan 21 '13 at 06:54
  • *Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.* — [Jamie Zawinski](http://en.wikiquote.org/wiki/Jamie_Zawinski) – Quentin Jan 21 '13 at 06:54
  • for email regex is needed. yup for other situation it may create two problems – Ravi Gadag Jan 21 '13 at 06:56
  • Email addresses are so complicated that [a regex to completely validate one](http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html) is insane. A simple "Is there an `@` in a sensible place, and (assuming public email addresses) is there a `.` in a sensible place?" is as good a check as any and better than most. – Quentin Jan 21 '13 at 06:58
  • The email regex is complex you have to start understanding from easier ones. Note from this answer it is complex and possibly not with it. – KJC2009 Mar 26 '14 at 22:11
  • here's a live demo: http://jsbin.com/ozeyag/19 – Superman2013 Mar 26 '14 at 22:57
  • 1
    @KJC2009 it was asked 1year ago. do you found your comment relevant to it now? – عثمان غني Mar 27 '14 at 06:42
  • @Superman2013 It was answered and solved one year ago. Then is it necessary to do some efforts on it? – عثمان غني Mar 27 '14 at 06:43

1 Answers1

4
var emailID = document.["myForm"]["email"].value;
                      ^^

Syntax error. Dot-notation or Square-bracket-notation. Pick only one (per property).

I'm surprised that Firebug / Chrome Developer Tools / Dragonfly / etc didn't give you a clear pointer to that when you were testing.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335