|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
I'm using the below line of code in my program to check if an email address found in a FORM value is valid, yet addresses like ^someth&ing@stuff.com^ are getting through the check OK. Why might the below not be working the way I expect? I thought my code only allows "word" characters since I'm using "/w" in my pattern. Am I missing something here?
============================================ if ( $FORM{'Email'} !~ /[w-]+@[w-]+.[w-]+/ ) { do something; } ==================================== Thanks in advance. -tbonds |
|
#2
|
|||
|
|||
|
Forget it, even the Perl Cookbook says that there is no viable solution to this problem.
|
|
#3
|
|||
|
|||
|
Maybe I can just use a for-loop and check each character for validity, besides checking something similar to ".*@*.*" for proper email construction.
If that's what it takes, then so be it. -tbonds |
|
#4
|
|||
|
|||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
sub ValidateEmail { # Initialize local email variable with input to subroutine. # local $email = $_[0]; # If the e-mail address contains: # if ($email =~ /(@.*@)|(..)|(@.)|(.@)|(^.)/ | | # the e-mail address contains an invalid syntax. Or, if the # # syntax does not match the following regular expression pattern # # it fails basic syntax verification. # $email !~ /^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$/) { # Basic syntax requires: one or more characters before the @ sign, # # followed by an optional '[', then any number of letters, numbers, # # dashes or periods (valid domain/IP characters) ending in a period # # and then 2 or 3 letters (for domain suffixes) or 1 to 3 numbers # # (for IP addresses). An ending bracket is also allowed as it is # # valid syntax to have an email address like: user@[255.255.255.0] # # Return a false value, since the e-mail address did not pass valid # # syntax. # return 0; } else { # Return a true value, e-mail verification passed. # return 1; } } [/code] |
|
#5
|
|||
|
|||
|
Instead of doing that, how about you make the user enter the valid address, a confirmation e-mail will be sent, if the user replies, then all is good and do what you want to do, if not then oh well, the user will be not signed up for whatever you are doing. This is the best approach.
|
|
#6
|
|||
|
|||
|
I agree.
I'm actually developing a shopping cart program that, among other things, emails the customer back all pertinent info regarding his order. Since most people can be careless at least some of the time, I'm adding a simple check for email validity. It won't be foolproof, but it should capture most gross mistakes. Regards, tbonds |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Validating email addresses using a regexp |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|