Validating name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Validating name

Post 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???
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. strlen()
  2. preg_match(), maybe?
  3. see above
  4. string.
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Post by kkonline »

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
}
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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You forgot to add spaces to the allowed list for preg_match().
Post Reply