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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Validating Email address tutorial 1

Status
Not open for further replies.

MaxGeek

Technical User
Jan 8, 2004
52
US
Hi I found a lot of email validating scripts on the net, but I would like a tutorial that would teach me how to make my own custom script. Does anyone know of a good tutorial.

I only need to validate the first part of an email address in the format "john.doe"

So I want to make my own to apply specificly to this.

I just don't understand the /^(\w¦\-¦\_¦\.)+\@((\w¦\-¦\_)+\.)+[a-zA-Z]{2,}$/) part of email validating scrips so I need a tutorial that teaches you how to write that part.

Thanks,
MaxGeek
 
if you want to write your own then firstly outline the checks you want to perform on the address then we'll start writing a regex to suit


Kind Regards
Duncan
 
^ starts with
here you go and to really learn the power of grep in perl you need the mastering regular exrepessions book by oreilly

(\w?\-?\_?\.) a word wirh - | _ | . in it
() captures the word to $1

+ at least 1 or more \@

((\w?\-?\_)+\.)+[a-zA-Z]{2,}$/)
() outter captures everything after @ to $2
() inner captures the word up to the . in $3

(\w?\-?\_)+\.) a word wirh - | _ | . in it
+ at least 1 or more [a-zA-Z] uc lc letters
{2,} with a minimun of 2 and no max amount
$ at the end of the line
 
sorry some extra crap pasted in

here you go and to really learn the power of grep in perl you need the mastering regular exrepessions book by oreilly

^ starts with
(\w\-\_\.) a word with - | _ | . in it
() captures the word to $1
+ at least 1 or more

((\w\-\_)+\.)+[a-zA-Z]{2,}$/)
() outter captures everything after @ to $2
() inner captures the word up to the . in $3

(\w\-\_)+\.) a word wirh - | _ | . in it
+ at least 1 or more
[a-zA-Z] uc lc letters
{2,} with a minimun of 2 and no max amount
$ at the end of the line

so you just have to use $1 to validate john.doe

 
Thanks everyone for your help. I'm going to work on it now.
 
Good luck and don't hesitate asking questions here. That enables me getting stars cheep ;)
 
Okay so I got the script working and then after playing with it I realized that my version isn't 100% fool proof.

the correct email address is "john.doe" (no need for domain)

Heres my code:
if (($email_address =~ /(\w\.)/) && (($email_address !~ /(\s)/)))

The problem is that it doesn't defend against "john." or "john.."

So my question is how do I split it at the "." and also how do i use quantifiers to limit the number of "."?
 
using RE:
Code:
if ($email_address =~ /(\w+\.\w+)/)
{
 print "email address\n";
} else
{
 print "wrong\n";
}
the + means 1 or more of the previous (1 or more word characters)
 
this will ensure you get only one dot

[red]\.{1}[/red]

Code:
@addresses = qw(john.doe john..doe);

foreach (@addresses) {
  if (m|\w+\.{1}\w+|) {
    print "accepted: $_\n";
  } else {
    print "rejected: $_\n";
  }
}

output:-

Code:
[blue]accepted: john.doe
rejected: john..doe[/blue]


Kind Regards
Duncan
 
No need for the {1}. \. will match one dot by itself, unless you have other quantifiers after it that enable it to match more than one.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top