Hello.

I am finally figuring out some basic validation.

My main interest at this point is validating email.

The script I got online contains the following, but does not check for "com", etc:

if (email.indexOf("@") == -1 || email.indexOf(".") == -1)

This works but does not check for "com". It MUST check for "com". So, I added this:

|| email.indexOf +2 > text.length )

To come up with this:

if (email.indexOf("@") == -1 || email.indexOf(".") == -1 || email.indexOf +2 > text.length )

The results: After adding the "." and submitting, there is no error message indicating that the field is missing "com".

This is imperative that I fix this.

Please advise.

Thank you in advance!

Matthew

Hi Matthew,

I'd suggest that you search on "javascript email validation" to find examples of how best to do this. Most of the solutions you find will rely on regular expressions to validate the address to a higher degree than your code, but you should still be able to find something that works for you.

Keep in mind that there are many Top Level Domain names (TLDs) other than '.com' (.net, .org, .info, etc.) and that there are many more coming soon. You might also consider using an HTML 5 <!DOCTYPE> and use <input type="email"> to let the user's browser validate the Email address for you.

Good luck!

commented: Thank you, Sir! +7

Hello Matthew,

I found this regex string a while back. unfortunately, i dont have the source to provide you with so you can go back and take a first hand look. I use it regulary when validating email addresses.

 function validateEmail(email)
 {
    var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return reg.test(email);
 }

The way it works is you simply pass the email address to this function and the function will return true or false.

So if you call the function and it returns false, you prompt the user to check their input and try again.

commented: Thank you, Sir! +7
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.