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???
Validating name
Moderator: General Moderators
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
- strlen()
- preg_match(), maybe?
- see above
- string.
feyd | Please use
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
,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
}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
,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]- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
You forgot to add spaces to the allowed list for preg_match().