...

View Full Version : php validation ereg!



simstar
03-07-2007, 08:43 PM
I am trying to validate entries server side from a form. I was hoping that this would only allow characters of the alphabet, but i seem to get the echo message returned everytime, no matter whether its within a-z or outside.


if (strlen($v) > 0){ // variety not required

if (!ereg("[a-zA-Z]+",$v))
{echo "This is invalid variety name.";
exit(0);}
}

Can anyone pick any errors or has any alternative solutions. Thanks.

bcarl314
03-07-2007, 09:06 PM
Try this...



if (strlen($v) > 0){ // variety not required

if (preg_match("/[^a-zA-Z]/",$v)) {
echo "This is invalid variety name.";
exit(0);
}
}

Inigoesdr
03-07-2007, 09:08 PM
if (strlen($v) > 0) // variety not required
{
if (ereg('[^A-z]+',$v))
{
echo 'This is invalid variety name.';
exit;
}
}
The circumflex will match anything -other than- an alphabetic character.

mmarif4u
03-08-2007, 04:14 AM
Try this one:

if ( !preg_match('/^[a-zA-Z\" "]*$/', $name) )
{
echo 'Name is not Valid Please try again.';
}

bcarl314
03-08-2007, 05:15 AM
Oh heck, this could quickly degenerate into a who can come up with the most convoluted regex ;)

You could also just do...



if(preg_match("/\W/",$v)) {


but that also allows spaces and underscores in the string.

simstar
03-12-2007, 06:54 PM
thanks for the help. Its working now.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum