Possibly:
$vaidateStr = "this is a test 1"; // not valid, contains spaces..
$compared = preg_replace("/[^a-z0-9\+\-]/", "", $vaidateStr);
$is_validated = $vaidateStr == $compared;
if ($is_validated) {
echo "valid<br />\n";
}
else {
echo "not valid<br />\n";
}
//
//OR just use (fewer lines, harder to read, etc)
//
if ($vaidateStr == preg_replace("/[^a-z0-9\+\-]/", "", $vaidateStr)) {
echo "valid<br />\n";
}
else {
echo "not valid<br />\n";
}
Ben
Hi,
I have an input string that should only be lower or upper alphas, numbers and can contain a hyphen.
I'm trying to figure out how to get PHP preg_match to verify the input string only contains these chars.
I tried some thing like this and it always returns true if I have one or more of the specified chars, even if I have a char that is not specified.
I need a true for any string that contains Alphas and/or numbers and/or one or more hyphens. I need it to return if the string contains anything else.
preg_match("/[a-z0-9\+\-]/", $vaidateStr)
I do not need to use preg_match, I'm looking for a simple solution.
Any pointers are much appreciated.
------------------------
Keith Smith
---------------------------------------------------
PLUG-discuss mailing list - PLUG-discuss@lists.plug.phoenix.az.us
To subscribe, unsubscribe, or to change your mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss