Validation Codes

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Validation Codes

Post by tecktalkcm0391 »

Can someone help me with this or explain it more.

I want to validate the variable $username. I want it to have only these values allowed:


A-Z
a-z
0-9
_

How could I do this? I got an idea, but I am so confused with the eregi(); command.

Also how can I make the variable $email have to be in the normal e-mail format with only these characters allowed:

A-Z
a-z
0-9
.
-
_



Thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You chose the perfect characters for the \w metacharacter.

Code: Select all

$var = 'sfdg6765_vgdz';

if (preg_match('/^\w+$/', $var)) echo 'Valid!';
As for the email question, there's a tonne of available pattern out there to use - have a search :)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Validation Codes

Post by Roja »

tecktalkcm0391 wrote:Also how can I make the variable $email have to be in the normal e-mail format with only these characters allowed
The RFC compliant email format uses more than just those characters. There are multiple RFC compliant validation formats that have been posted on these forums, including one I use.
Post Reply