Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Javascript Expression To Validate E-Mail Is Too Strict

JavaScript validate

  • Please log in to reply
5 replies to this topic

#1 mctim

mctim

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 20 June 2012 - 03:33 PM

I found this expression on the internet that I've been using to validate e-mail addresses:

var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
if (email.value.search(emailRegEx) == -1) {
	  alert('You must enter a valid e-mail address');
	  return false;
}

However if you have an e-mail like nobody.foo.bar@gmail.com it will say that the e-mail is invalid. Can anyone see why?
  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 20 June 2012 - 04:12 PM

There is no problem with that regular expression, your problem is you need to trim the email, cause email.value probably have either leading or trailing spaces.

try to use the regex with this hardcoded string : 'nobody.foo.bar@gmail.com' you will see it works, but if you add an space either ahead or at the end it will stop working ;)

if you don't know the regex for trimming a string just make me know
  • 0

#3 mctim

mctim

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 20 June 2012 - 04:18 PM

Yeah so how do I trim a string?

BTW I just tested it and that email still caused the error message to display. I even made sure that there was no leading or trailing whitespace.
  • 0

#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 20 June 2012 - 04:43 PM

this perfectly works :


var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
var email = 'nobody.foo.bar@gmail.com';

if (email.search(emailRegEx) == -1)
{
	  alert('You must enter a valid e-mail address');
}
 

give me a minute i will go make the triming

Here, a very stupid demo of the space trimming regex, if you quit that regex, you will find out that the spaces makes your regex not to work, still, it might be other chars interfering


var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;

// the spaced email
var email = ' nobody.foo.bar@gmail.com ';

alert( "<"+email+">" );
email = email.replace( /(^[\s	]{1,}|[\s	]{1,}$)/g , "" ) ;

// the unspaced email :D
alert( "<"+email+">" );

if (email.search(emailRegEx) == -1)
{
	  alert('You must enter a valid e-mail address');
}
 

check it up, and then, if it does not work just post the whole html, maybe i can find what is going wrong there :D
  • 0

#5 mctim

mctim

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 25 June 2012 - 11:52 AM

I changed my code to this

var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
   var emailString = email.value;
   email = email.replace( /(^[\s   ]{1,}|[\s	   ]{1,}$)/g , "" ) ;
   if (emailString.search(emailRegEx) == -1) {
    alert('You must enter a valid e-mail address');
    return false;
   }

now when I run the "replace" function my form will submit immediately and will not continue to check the other fields.
  • 0

#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 25 June 2012 - 03:48 PM

oh, there is a problem with the forum, it is taking tha tab character (bar)t and printing a tab instead,

i will rewrite it, you replace (bar) with \

/(^[(bar)s(bar)t]{1,}|[(bar)s(bar)t]{1,}$)/g
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download