On 3/9/02 1:46 PM, Scot Robnett <[EMAIL PROTECTED]> wrote: Hi Scot,
> I'm trying to do a simple verification of an e-mail address format. I want > to require: > > - 1 or more alphanumeric characters > - followed by "@" > - followed by 1 or more alphanumerics > - followed by a dot > - followed by 2-4 alphanumerics. > (for example, .tv, .com, .info) What about things like ".co.uk"? And what about things like <[EMAIL PROTECTED]>? I'm not that well-versed in what makes an email address valid, but I'm sure more than a few valid email addresses will be rejected by the above requirements. > If the string doesn't fit these requirements, then I send them back to the > form to re-enter the address. The problem is that using the code below, the > program complains even when the address *is* formatted properly. Any ideas > how I could reformat the expression better? Yes. > ########################################################## > > if(($email !=~ /\w+[@]\w+\.\w{2}/) or ($email !=~ /\w+[@]\w+\.\w{3}/) > or ($email !=~ /\w+[@]\w+\.\w{4}/)) > { > print "Improperly formatted e-mail address! Please use your browser\'s back > "; > print "button and make sure the e-mail address is entered correctly. "; > } > > ########################################################## First off, I'd recommend reading through the "pattern matching" section of the Camel Book (Programming Perl, by Larry Wall, et al, published by O'Reilly) again. There's a lot to learn. Now, a slightly better-working version of the code above: ##### CODE ##### unless($email =~ /^\w+(\.\w+)*\@\w+(\.\w+)*\.\w{2,4}\z/){ # error stuff } else{ # email-ok stuff } ##### END CODE ##### to examine that regex: ^\w+(\.\w+)*\@\w+(\.\w+)*\.\w{2,4}\z ^ = the beginning of the string \w+ = one or more word chars (\.\w+)* = any number ("any number" includes zero) of sets of a dot and one or more word chars \@ = @ sign \w+ = one or more word chars (\.\w+)* = one or more word chars followed by any number of sets of a dot and one or more word chars \. = a dot \w{2,4} = one or more word chars \z = the end of the string I am NOT, however, saying that this regex necessarily matches all valid email addresses, or that it screens out all invalid ones. It doesn't. And nothing prevents users from entering something like "[EMAIL PROTECTED]". hth, -- Michael -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]