Page 1 of 1
Validating name
Posted: Tue Aug 21, 2007 9:54 am
by kkonline
How do i validate the name field $_POST['name'] for following
Maximum of 30 chars.
Allow - and ' chars as there are name O'Reilly and Mohammad-ShaH
All other cases invalid
Another thing if i have <input type="text" value="name" size=30> then whatever the user enters that will be taken as a string or array or that depend on the input???
Posted: Tue Aug 21, 2007 9:58 am
by feyd
- strlen()
- preg_match(), maybe?
- see above
- string.
Posted: Wed Aug 22, 2007 2:40 am
by kkonline
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Code: Select all
$n = $_POST['name'];
if (strlen($n) < 31 && preg_match("/^[a-zA-Z'-]+$/", $n)) {
// $n is valid
}
else {
// $n is not valid
}
It works for only one name .
Now if i have a name like John Bell Rod then it doesnot validate.
It should validate the spaces, - and ' only and 31 character long only
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Wed Aug 22, 2007 7:17 am
by feyd
You forgot to add spaces to the allowed list for
preg_match().