Email Validation

Expert User Verified

Email validation in javascript.

<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>

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)">

References and Credits

Comments

Leave a Comment