2

Having troubles with my email validation code. I keep on getting the error that my function is not defined. i have made a javascript file for the java code and then i used the onchange in my html to trigger the function.

    <input type="text" id="email" name="email" onchange="check();" />

    function check() {
email = document.getElementById("email").value;
filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) 
    {
    document.getElementById("email").style.border = "3px solid green";  
    return true;
    }
else
    {
    document.getElementById("email").style.border = "3px solid red";
    return false;
    }
}
3
  • 1
    Since when was + an invalid character in email addresses? And why can't a TLD have more than 4 characters in it?
    – Quentin
    Dec 23, 2011 at 0:18
  • +1 from me. No need to down vote. OP is asking for why getting javascript error which is not even caused by the regex.
    – Ray Cheng
    Dec 23, 2011 at 0:21
  • Sorry im still not used to the way that i have to post a question
    – MunLau
    Dec 23, 2011 at 0:27

4 Answers 4

4

Put your javascript in <script> tags.

Also rename your variable name email since your textbox is using it already.

<input type="text" id="email" name="email" onchange="check();" />
<script type="text/javascript">
    function check() {
        var email_x = document.getElementById("email").value;
        filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (filter.test(email.value)) {
            document.getElementById("email").style.border = "3px solid green";
            return true;
        } else {
            document.getElementById("email").style.border = "3px solid red";
            return false;
        }
    }
</script>
1

Email validation is not always as simple as your regular expression. Have you looked at:

Validate email address in JavaScript?

A better option would be to use Verimail.js. It's a simple script that takes care of it for you. With Verimail.js you could just do:

var email = "[email protected]";
var verimail = new Comfirm.AlphaMail.Verimail();

verimail.verify(email, function(status, message, suggestion){
    if(status < 0){
        // Incorrect syntax!
    }else{
        // Syntax looks great!
    }
});

The above example will hit the line 'Incorrect syntax!' because of the invalid TLD 'cmo'. Besides this, it will also give a suggestion that you can return to your user, in this case, the suggestion variable will contain '[email protected]' since 'fabeook.cmo' looks a lot like 'facebook.com'.

Hope this helps!

1

Here is the code for html input field and button field

   <input input type="text" name="txtEmailId" id="txtEmailId" /> 
   <input type="submit" class="button" value="Suscribe" name="Suscribe" 
            onclick="javascript:ShowAlert()" />

Now add the below function to the header of your page

 <script type="text/javascript">
 function ShowAlert() {
  var email = document.getElementById('txtEmailId');
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
        alert('Please provide a valid email address');
        email.focus;
        return false;
    }
    else {
        alert("Thanks for your intrest in us, Now you 
        will be able to receive monthly updates from us.");
        document.getElementById('txtEmailId').value = "";
    }
 }
 </script> 

Here you can find the article on this Email Validation in JavaScript

0

If you are specific about the domains , you may use this code for email validation so as to prevent anonymous email domains.

(^([a-zA-Z]{1,20}[-_.]{0,1}[a-zA-Z0-9]{1,20})(\@gmail\.com|\@yahoo\.com|\@hotmail\.com)$)

You may add additional domains too.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.