Re: Regular Expression Validation Puzzle
Hi David,
Thanks very much for taking the time to respond and thanks for the tip re.
ereg - I'll make a point of using preg from now on and yes the max feature is
now working. However you're testing repeatedly, it goes into a state where it
always reports an error - e.g.'Enter valid contact name: Jo O\'Kane' (the '\'
character is added by the system - see below) ?
I thought I'd twigged my own problem re. removing extra spaces when I came
across using 'explode' & 'implode' but your solution is much neater.
Would you mind explaining what is happening here as it is not obvious (to me
anyway) as I would like to be able to use this technique again but I can't
understand the statements although obviously I can see what they are doing.
PS. I the interim period I raised another query about the recording names with
an apostrophe (e.g. O'Reilly) in a db.... basically it looks as if the name
will be recorded as O\'Reilly ... any thoughts?
I'm now using this code based on what you sent me previously ...
$errors = array();
$val_cn = trim(preg_replace('/\s+/', ' ', $_POST['contact_name']));
if (!preg_match("/^[-a-z'\s]{5,10}$/i", $val_cn)) {
$val_cn = stripslashes($val_cn);
$val_cn = mysql_real_escape_string($val_cn);
$cn_html = htmlentities($val_cn);
{ $errors [] = 'Enter valid contact name: ' . $val_cn . ' ' . $cn_html; } }
will yeild 'O\'Reilly'
Thanks again - great stuff.
Patrick
|