Forum Moderators: coopster

Message Too Old, No Replies

validating the length of a string

         

big_jimmi

5:06 pm on Jan 22, 2007 (gmt 0)

10+ Year Member



Hi folks

I hope you can help.

Im sure this is quite easy to do but being very new to php im a little stuck. I'm wanting to ensure the length of a string entered by the user is >0 and <12 (ie a telephone number.) Im also wanting to ensure that the users cant enter any non numerical data and that one of the phone fields CAN be blank (ie they only have to provide one telephone number. This is what ive got so far -

if (empty($_POST['homephonefirst']) && empty($_POST['mobilenumberfirst'])){
$error_msg['homephonefirst'] = '<div class=message>Please enter at least one contact telephone number</div>';
} elseif (ereg('^[^0-9 ]+$' , $_POST['homephonefirst'])){
$error_msg['homephonefirst'] = '<div class=message>Invalid character in Home telephone number</div>';
} elseif (ereg('^[^0-9 ]+$' , $_POST['mobilenumberfirst'])){
$error_msg['homephonefirst'] = '<div class=message>Invalid character in Mobile telephone number</div>';
}

Can someone point me in the right direction?

Many many thanks in advance

jatar_k

5:07 pm on Jan 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use strlen() [php.net]

the rest looks ok

big_jimmi

5:27 pm on Jan 22, 2007 (gmt 0)

10+ Year Member



sorry exactly where would i put that - and it still lets me put letters after the numbers ie 0208lkjlkjk - any ideas?

jatar_k

5:54 pm on Jan 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well, I seldom use regex so I would probably do something like

$home = $_POST['homephonefirst']; 
$mobile = $_POST['mobilenumberfirst'];
if (empty($home) && empty($mobile)){
$error_msg['homephonefirst'] = '<div class=message>Please enter at least one contact telephone number</div>';
} else if (!empty($home) &&!ctype_digit($home)){
$error_msg['homephonefirst'] = '<div class=message>Invalid character in Home telephone number</div>';
} else if (!empty($mobile) &&!ctype_digit($mobile)){
$error_msg['homephonefirst'] = '<div class=message>Invalid character in Home telephone number</div>';
}

[php.net...]

you could also look at this
[webmasterworld.com...]