Add to collection
Expert User

Expert User

Verified

Email Validation

Email validation in javascript.

Try Yourself in Code Editor    

It validates the input whether the correct format of email has been entered in the textbox or not. If the email format is not valid then it clears the text box. 

Call the javascript function on change event of the textbox.

<input type="text" id="inputText" name="inputText" onchange="return validation(this.id)">
<input type="text" id="inputText" name="inputText" onchange="return validation(this.id)">
<script type="text/javascript">
        function validation(inputText)
            {
                var email=document.getElementById(inputText).value;
            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            if(email.match(mailformat))
            {
           
            return true;
            }
            else
            {
            alert("You have entered an invalid email address!");
            document.getElementById(inputText).value="";
            return false;
            }
            }
        </script>

References and Credits

Comments 0

  • Sorry!! No comment posted yet. Become the first user to give comment.
Add a comment

Would you like to write for w3 sniff?

Become part of an amazing group of contributors and authors and start writing for us

Join Now
<input type="text" id="inputText" name="inputText" onchange="return validation(this.id)">
<script type="text/javascript">
        function validation(inputText)
            {
                var email=document.getElementById(inputText).value;
            var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            if(email.match(mailformat))
            {
           
            return true;
            }
            else
            {
            alert("You have entered an invalid email address!");
            document.getElementById(inputText).value="";
            return false;
            }
            }
        </script>
Feedback submitted successfully.