|
Posted by Pedro Graca on 10/11/06 14:17
Mark Woodward wrote:
> I'm trying to validate text in a HTML input field.
> How do I *allow* a single quote?
The following code works for me
<?php
// catch any nasty characters (eg !@#$%^&*()/\)
$match = '/^[\'\w\._?!, -]+$/';
$res = 'word';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
$res = 'two words';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
$res = 'pseudo-word';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
$res = 'Mc\'Donalds';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
$res = 'under_score';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
$res = 'Question? Answer!';
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
$res = "embedded\ttab";
echo 'Search: "', $res, '" is ', (preg_match($match, $res))?('valid'):('not valid'), "\n";
?>
> // catch any nasty characters (eg !@#$%^&*()/\)
> $match = '/^['\w\.\-_?!, ]+$/';
// * 1 22
*1 the single quote here is invalid; you need to escape it
*2 I'm not sure an escaped "-" works. What I do when I want a "-" in
a regular expression inside a class definition is to put it at the
end
--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
[Back to original message]
|