Page 1 of 1
Validating Name Input that contains apostrophes and hyphens
Posted: Thu Jun 09, 2005 4:16 pm
by BHarris
I am fairly new to php and using the functions correctly to validate certain information. I know that this is fairly simple to do but when I print out my data which is taken from a database names such as O'Hara comes out with 7 slashes in front of the apostrophe. The name is stored in the database (mysql) as is "O'Hara". I basically need an idea so I may see what is the best way to to apply in trying to fix that problem. I presently have this little thing that I am using, however, it is not working correctly.
Code: Select all
<?php
function check_name(&$name)
{
$name = trim($name);
if(((eregi('^[A-Za-z]+[\'\-]?',$name) === FALSE)))
return FALSE;
return TRUE;
}
?>
My Result is something like this:
Well, I hope I can get this little problem fixed. Thanks
Posted: Thu Jun 09, 2005 4:19 pm
by Burrito
stripslashes() is your friend

Posted: Thu Jun 09, 2005 4:22 pm
by Skara
erhm.. first, that's a lot of (). second, preg_match is faster. third, your regex is really goofed.
Code: Select all
function check_name(&$name) {
$name = trim($name);
if (!preg_match('/^[a-z\'\-]+$/i',$name)) return false;
return true;
}
but yeah, stripslashes will get that other problem fixed.

Follow up to my name validation
Posted: Fri Jun 10, 2005 10:07 am
by BHarris
Thanks for the info you provided. However, when I try this new function using the "preg_match" function the way that you have suggested, I am unable to enter names such as "O'Hara" (it goes to my error message that points out that the name is invalid). What do you think needs to be changed in order to accept and name such as "O'Hara"?
Storing data into the database without slashes
Posted: Wed Jun 15, 2005 9:09 am
by BHarris
Stripslashes(), works very well to get rid of the slashes when viewing the data that had been slashed. However, for some reason these slashes are still on when when I insert this data into the database as part of my "insert_event()" function that was written. Does anyone know why these slashes (as many as 15) would be added to the data when it is being sent to the database even though it does not have the slashes when viewed just before being sent to the database? If anyone knows, what is the best way to get rid of that problem?
Thanks.