Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

email validation

 
MInu
Ranch Hand
Posts: 517
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..

can anyone help me to validate my forms email adress field.
i am using this code.. but it doesnt work


function validEmail(_form,_action)
{

if(document.euserForm.popUserID.value=="")
{
alert("Enter your email id");
return
}
else
{
if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.euserForm.popUserID.value)))
{

alert("The email you entered is not a valid email address.");
return
}
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was at a course last month on javascript and one of the things I got was email validation, I kept this just incase I needed it again. maybe you could change your regexp string to:
/(\w*\@\w*\.\w{3})|(\w*\@\w*\.\w{2}\.\w{2})|(\w*\.\w*\@\w*\.\w{3})|(\w*\.\w*\@\w*\.\w{2}\.\w{2})/g

Davy
 
Lasse Reichstein Nielsen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recognizing e-mail addresses is hard, bordering on impossible. The only real reference to what an e-mail address can look like is the SMTP specification (RFC 2821), and that is very forgiving. Anything on the form <localpart>@<domain> is acceptable.

I usually recommend going with just:


That accepts all of these (perfectly valid) addresses:
  • me+something@example.com
  • me.something@[192.168.10.2]
  • "Mr.Jones"@example.com


  • The parts before the "@" should only ever be interpreted by the
    receiving host, so you shouldn't try to rule something out (except
    extra "@"'s). The part after the "@" can be a domain name or an
    IP-address (both IPv4 and IPv6).

    Also remember, that there is no way to ensure that an e-mail address works, i.e., that it can receive mail, except trying to send to it and receive an answer. On the other hand, it is far too easy to reject a perfectly good and working address, which will make the user of the page a lot of pain and cursing ... not something to aim for.

    Aim to accept too much rather than too little. The worst that can happen is that the mail bounces or is lost, and that will probably happen for foo@example.com too.

    /L
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic