hi all

I am doing a form where i have to validate the email, all of the other control is working just the way it is supposed to work but I have been trying to validate the email for the longest while and it is not working properly or better yet it is not validating, I dont know if it is the codes that I am using or if there is an error there that I am not seeing, can someone help me out and let me know if there is an error here is the codes that I am using.

function ValidateForm()
{
    var a, i, error, check, title, firstname, lastname, address;
    var txtemail, gender, userName, pword, cword;

    a=0;
    error=false;
    check=false;

    title=document.getElementById("title").value;
    firstname=document.getElementById("firstname").value;
    lastname=document.getElementById("lastname").value;
    address=document.getElementById("address").value;

    if(title=="" || title==null)
    {
        alert("Please select a title");
        error=true;
        return false;
    }
    if(firstname=="" || firstname==null)
    {
        alert("Please enter your first name");
        error=true;
        return false;
    }
    if(lastname=="" || lastname==null)
    {
        alert("Please enter your  last name");
        error=true;
        return false;
    }
    if(address=="" || address==null)
    {
        alert("Please enter your address");
        error=true;
        return false;
    }

}
function IsValidEmail(txtemail)
{
    var emailregex = new regex(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
    var valid = emailregex.test(txtemail);
}

function ValidateEmail()
{
    var txtemail = document.getElementById("txtemail").value;

    if(IsValidEmail(txtemail))
    {
        alert("Invalid email address");
    }
    else
    {
        alert("valid email address");
    }
}

Recommended Answers

All 5 Replies

Personally I'm always amazed that any regex more than about a dozen chars ever works.
Anyway, maybe you could try this link, which has regex for various languages that they claim to be 99.99% perfect according to RFC5322 ...
http://emailregex.com

hi james

so you are saying with amt of char will not work ok

No no. Regexs will work with hundreds of chars. I was just saying that when they get long they are so hard to read and understand. So it's amazing that anyone can get them right! Some of the examples on the link I posted are even longer, but they are all tested and working OK.

Your function IsValidEmail does not return anything, and your application of it expects a boolean.

If your regex test validates true, you should return true. If it doesn't validate, return false.

Use the standard method. It's much cleaner, nicer, simpler and more robust.

<input type="email" ... />
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.