Forum Moderators: coopster

Message Too Old, No Replies

Validating name

         

kkonline

2:52 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



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?

d40sithui

3:12 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



you can use the strlen function to chek the lenght of any string.
so u can do something like

<?
if(strlen($_POST['name']) > 30){
return false;
}
?>

also u can limit on the form the maximum length by:
<input type=text name='name' size=30 maxlength=30>

in regards to checking for the dash(-) and quote('), you'll need regualr expresions, im bad at that. maybe someone else can chime in

kkonline

7:35 am on Aug 22, 2007 (gmt 0)

10+ Year Member



$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

jatar_k

12:30 pm on Aug 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



add the space then

from
if (strlen($n) < 31 && preg_match("/^[a-zA-Z'-]+$/", $n)) {

to
if (strlen($n) < 31 && preg_match("/^[a-zA-Z'- ]+$/", $n)) {

d40sithui

2:33 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



jatar_k, as always you kick ass.

jatar_k

2:34 pm on Aug 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



aw shucks ;)