methuselah
methuselah

Reputation: 13216

Validate an email address

How would I check that a user's email address ends in on of the three

@camel.com, @mel.com, @camelofegypt.com

This is my code so far, it just validates whether or not the user has provided any text

if ($.trim($("#email").val()).length === 0) {
alert('You must provide valid input');
return false;

I want to implement the email address validation into my code.

Upvotes: 1

Views: 399

Answers (7)

abc123
abc123

Reputation: 18853

As described in the library to regular expressions, it is difficult to truly validate an email address. However, the below taken from the above website will do a good job.

The official standard is known as RFC 2822. It describes the syntax that valid email addresses must adhere to. You can (but you shouldn't--read on) implement it with this regular expression:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Simple Regex:

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Trade offs of validating email addresses:

Yes, there are a whole bunch of email addresses that my pet regex doesn't match. The most frequently quoted example are addresses on the .museum top level domain, which is longer than the 4 letters my regex allows for the top level domain. I accept this trade-off because the number of people using .museum email addresses is extremely low. I've never had a complaint that the order forms or newsletter subscription forms on the JGsoft websites refused a .museum address (which they would, since they use the above regex to validate the email address).

However, if you just want your specific domain this is definitely a possibility but it is not recommended to deny an email address because it fails these regular expressions.

Taking the above you could simply validate using the following Regex:

\b[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com\b

or:

^[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com$

The difference between these two regex are simple, the first regex will match an email address contained within a longer string. While the second regular expression will only match if the whole string is the email address.

JavaScript Regex:

/\b[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com\b/i

or:

/^[A-Z0-9._%+-]+@(camel|mel|camelofegypt)\.com$/i

Special Note: You should likely allow for case insensitive with your regex using the i parameter since [email protected] is the same as [email protected]. Which i've done in the above regex.

Upvotes: 2

Jignesh.Raj
Jignesh.Raj

Reputation: 5987

function ValidateEmail(email) {
 var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

OR

This is a regular-expression email validation comparison that will test a bunch of valid/invalid email address against the regex provided by you in the textarea below.

^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$

Upvotes: 1

SoWhat
SoWhat

Reputation: 5622

use the following regex:

var reg=/@(camel|mel|camelofegypt)\.com$/
 reg.test(email)

This only validates if you email ends in one of the three above. If you want ot know general email validation, search the web. There are tons of those

Upvotes: 3

Ajay
Ajay

Reputation: 6590

using new regex

demo Here

added support for Address tags (+ sign)

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
return pattern.test(emailAddress);
};

Note : Keep in mind that no 100% regex email check exists

Upvotes: 0

Antony Johnson
Antony Johnson

Reputation: 71

var x=$("#email").val();
var n=x.split("@");
if($.inArray(camel.com,n)!= -1)//checks if camel.com is there in your email address if not  the value will be -1 @is not given in front of camel.com because its split after @ which means that @will be present before it if camel.com is prescent.
  {
}                                   
else
if($.inArray(mel.com,n)!= -1)
{}
else
 if($.inArray(camelofegypth.com,n)!= -1)
{}
else
   alert("");

Upvotes: 0

Shijin TR
Shijin TR

Reputation: 7788

Try this function,

 function validateEmail($email) {
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  if( !emailReg.test( $email ) ) {
   return false;
 } else {
  return true;
}
}


if( !validateEmail(email)) { /* do stuff here */ }

Upvotes: 0

Pandiyan Cool
Pandiyan Cool

Reputation: 6585

Try out this javascript

function IsEmail(email) {
 var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

Upvotes: 0

Related Questions