...

View Full Version : Using RegEx to only allow one lowercase letter?



cyphix
09-21-2005, 02:11 PM
I'm still a bit of a noob @ using regex so I can't work it out.... tried the below coddes but they both had problems..



if (preg_match('/[^a-z](1)/', $char)) {
die('Invalid Request');
}


That still allowed numbers of what I tested....



if (preg_match('/[^a-z(1)]/', $char)) {
die('Invalid Request');
}


That didn't allow numbers; but did allow more than one letter.

Any help? :confused:

NancyJ
09-21-2005, 02:37 PM
not exactly sure what you're trying to do here. Do you mean that the string can contain letters and numbers but only 1 of those characters can be a lowercase letter?
If so... this should do the trick



if (preg_match('/[a-z]{2,}/', $char)) {
die('Invalid Request');
}

or do you mean you only want 1 character and it must be lowercase? (are numbers allowed?)


if (!preg_match('/^[a-z]$/', $char)) {
die('Invalid Request');
}
or if it can be a lowercase letter OR a number


if (!preg_match('/^[a-z0-9]$/', $char)) {
die('Invalid Request');
}

cyphix
09-21-2005, 03:55 PM
Thanks Nancy.. this would be the one..



if (!preg_match('/^[a-z]$/', $char)) {
die('Invalid Request');
}


As for this code..



if (preg_match('/[a-z]{2,}/', $char)) {
die('Invalid Request');
}


Yeah.. that's what I was trying to do with mine; but with just the number 1 in there.... guess I didn't look at the tutorial I have been reading close enough to notice they were curly braces & not parentheses.

Cheers! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum