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

eregi & pattern matching

Status
Not open for further replies.

dodgyone

Technical User
Jan 26, 2001
431
GB
I'm trying a simple data validation script to ensure that my users are entering the data in the correct fashion.

I have a name text box where the users enter a 2 or 3 character initial. They must add a space inbetween each initial, so:

MJ WAS TY BT ...etc etc etc

They must enter at least one initial and can enter up to 5 initials in all.

I have a script to check the first initial to ensure that it's alright.

if (!eregi("(^[A-Z]{2}[A-Z]?[[:space:]]?$)", $name))

This works as I want it to but how do I add on the optional extra 4 initials that may or may not be added?

Hope this makes sense... thanks for listening
 
try ([A-Z]{4})?

the ? means optional. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Thanks Anikin I just figured that one out ;o)

I've gotten as far as:

"(^[A-Z]{2}[A-Z]?[[:space:]]?)([A-Z]{2}?[A-Z]?[[:space:]]?$)"

Is there an easier way to create this? I'm going to have 5 of these in the script which is quite bulky!!!!!!

Or am I just being silly?
 
no ... but is a great mess you're doing.

why [A-Z]{2}[A-Z]? instead of [A-Z]{2,3}

this is the same thing.

then you have a space optional?

this should be something like:

^[a-z]{2,3} ([a-z]{4})? ...

this way you can get 2 or 3 initials and the 4 word optional after the space. NOTE ON THE SPACE I LEFT BETWEEN THEM. You said the user must add the space after the first initials Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top