Jump to content

[SOLVED] restricting number of characters


php101

Recommended Posts

Hi, I neeed to restrict an input field in a form to maximum 8 characters.

Can you help by adding in green color to the following code?

Thanks a lot.

 

<td><?=$s_lang['midname'];?>: </td>    <td><input type="text" class="main_text" name="midname" style="width:90%;" value="<?=$_POST['midname'];?>" /></td>

 

Link to comment
https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/
Share on other sites

Tack on a "size='8'" to the HTML input field to remind the user that it only accepts 8 characters.

<?php
<td><?=$s_lang['midname'];?>: </td>    <td><input type="text" size="8" class="main_text" name="midname" style="width:90%;" value="<?=$_POST['midname'];?>" /></td>
?>

 

 

Then check the lenght of the string in PHP like this:

<?php
$midname = trim($_POST['midname']);
if (strlen($midname) > 
{
  // Complain or cut the variable down to 8 characters
}
?>

 

 

http://us2.php.net/strlen

http://us2.php.net/manual/en/ref.strings.php

 

 

 

You could use the size="8" anyways just so they do not misunderstand.

 

But be sure that you check the length in PHP. The is the one place that counts.

Is there really any point in checking it in PHP if you have the maxlength property?  I do not think there is a way to get around that.

 

If you are not concerned about security you can skip the PHP lenght check, but it's a good habit to always check you script input.

 

PS: I have a Firefox extension that overrides all of the properties like that in a HTML form and lets me submit what ever I want. There are many other ways to do that too.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.