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

Cannot get preg_match to work

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I have a form in an app that I inherited for creating email addresses. The form passes the user part , the domain part separately to a function that puts them together to make an email address and then validates everything up to the @. Problem is some admins have been putting the whole email address in the user part of the form so I need to look at that part and if there is an @, then return a message stating that there are invalid characters. I have this expression, but it seems to not work.

Code:
trim($user);
   if (! preg_match("/[a-zA-Z0-9_\-\.]/",$user))
    {
     return "User $user contains invalid characters<br>";
    }

Any ideas on what I am doing wrong? I believe that this says if $user is not a-z, A-Z, 0-9, an _, a -, or a . that it should return $return.
 
Hi

Misplaced negation.
You said : if not contains any of the enumerated characters.
Should say : if contains any of the not enumerated characters.
Code:
[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]preg_match[/color][teal]([/teal][green][i]"/[[highlight]^[/highlight]a-zA-Z0-9_\-\.]/"[/i][/green][teal],[/teal][navy]$user[/navy][teal]))[/teal]
    [teal]{[/teal]
     [b]return[/b] [green][i]"User $user contains invalid characters<br>"[/i][/green][teal];[/teal]
    [teal]}[/teal]


Feherke.
 
Excellent! That worked. I am ok with basic php programming, but I am a uber novice when it comes to using preg_match
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top