Jump to content

count the characters


thibo1025

Recommended Posts

I created a form that will send information to my database but in various steps so the information send in the first step might be too long for example:I have a variable $v and i do not want it to be longer than 10 characters. my code looks like this.

<form action='?' method='post'><input name='v' type='text' /><input type='submit' /></form><?include('function_count.php');$v = $_REQUEST['v'];count($v,$w); //Where $w would be the numbr of characters of $v.if($w > 10){die(that info is too long!)}else{/*rest of the page*/}?>

My question is infortunately what should contain the 'funtion count()' in function_count.php.

Link to comment
Share on other sites

On the user side, you will want to do this:<input name='v' type='text' maxlength='10' />Also, the function count is a pre-defined function, it is used for getting the number of elements in an array. So you will need a different name.

function check_length ($str, $len){  if (strlen($str) > $len)	return false;  return true;}...if(check_length($v, 10) == false){  die("that info is too long!");}

Or you can shorten it altogether and use the built-in strlen function:

if(strlen($v) > 10){  die("that info is too long!");}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...