Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression 1

Status
Not open for further replies.

arst06d

Programmer
Nov 29, 2002
324
Hi
Hope someone can help. I need to validate email addresses so that only certain entries are accepted as valid

eg abc@mydomain.com is OK, as is def@mydomain.com but anything else is invalid.

would (abc|def)@mydomain.com do it?

How would I write an expression to filter out anything BUT abc or def?

Many thanks in advance
 
You're on the right track. You just need to check for start and end of string, and make sure you escape any special characters that might cause problems (specifically the period)
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>title test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

var regex = /^(abc|def)\@mydomain\.com$/;

var a = "abc@mydomain.com";
var b = "def@mydomain.com";
var c = "xyz@mydomain.com";
var d = "abc@mydizzomain.com";

alert(a + ": " + regex.test(a));
alert(b + ": " + regex.test(b));
alert(c + ": " + regex.test(c));
alert(d + ": " + regex.test(d));


</script>
<style type="text/css"></style>
</head>
<body>

</body>
</html>

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
WOW - that was fast!

Many thanks for your help.

Beautiful puppies, by the way. Never heard of Silky Terriers - I have a couple of Cairns myself ...

Thanks again.
 
I know kaht has already answered this, he had to of.

You can use the test() method to evaluate your RegEx, if it's valid, just return the opposite of to find any illegal e-mails.


Code:
function testEmail(emailVariable) {
   if (!(/^(abc|def)@mydomain\.com$/.test(emailVariable))) {
      alert("ILLEGAL E-MAIL!!!");
      return false;
   }
}

Here is an example function that does this (tested).

[monkey][snake] <.
 
arst06d, thanks for the star. That was #500 for me in this forum [thumbsup2]

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top