PDA

View Full Version : regular expression problem



rogermsw
12-05-2009, 07:59 PM
I have the following as a validation check on a search field,
The person entries a name between 3 - 12 characters long and the function checks to see if it's only a string of letter and everything works fine.
Now however I need to add in a single quote " ' " I've tried every combination I can think of and none of them work my best guess was [^a-zA-Z'] but that did not work and I've tried tons but get no where... help
Peter :confused:

original code:

function is_alpha($someString) //function to check for letters
{
return (preg_match("/[^a-zA-Z]/", $someString) > 0) ? true : false;
}

$element = $_POST['name'];
if (strlen($element) <3 or strlen($element) >12)// check letters entered is between 3-12
{
header("location:searchform.php?err=1"); // return error call
}
if(is_alpha($element)) //check that only letters entered
{
header("location:searchform.php?err=2"); // return error call
}

jscheuer1
12-05-2009, 09:21 PM
This:



/[^a-zA-Z]/

Matches any single character in the string that is not a-z or A-Z. And I see you are looking at it from the point a view that if there is a match, that means it's invalid. Now it might be as simple as escaping the ':


/[^a-zA-Z\']/

But since the RE is already a string, a double escape may be required:


/[^a-zA-Z\\']/

Have you tried those?

rogermsw
12-06-2009, 12:12 AM
Hi John

Have tried both we no joy.

james438
12-06-2009, 02:14 AM
I am terrible at functions, but try

/[^a-zA-Z\\\']/