php101 Posted April 18, 2008 Share Posted April 18, 2008 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 More sharing options...
947740 Posted April 18, 2008 Share Posted April 18, 2008 <input type="text" class="main_text" name="midname" style="width:90%;" value="<?=$_POST['midname'];?>" maxlength="8" /> Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-520830 Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 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 Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-520831 Share on other sites More sharing options...
dptr1988 Posted April 18, 2008 Share Posted April 18, 2008 Oops! I was wrong!! Use 'maxlength=8' in HTML instead of 'size=8' But be sure that you check the length in PHP. The is the one place that counts. Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-520835 Share on other sites More sharing options...
947740 Posted April 19, 2008 Share Posted April 19, 2008 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. Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-520840 Share on other sites More sharing options...
dptr1988 Posted April 19, 2008 Share Posted April 19, 2008 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. Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-520850 Share on other sites More sharing options...
947740 Posted April 19, 2008 Share Posted April 19, 2008 Ah. You learn something new every day. Never mind my previous post then. Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-520855 Share on other sites More sharing options...
php101 Posted April 20, 2008 Author Share Posted April 20, 2008 Thanks very much dptr2988 and 947740. The page has been updated and it is working okay. Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-521684 Share on other sites More sharing options...
947740 Posted April 21, 2008 Share Posted April 21, 2008 Glad to help. Link to comment https://forums.phpfreaks.com/topic/101791-solved-restricting-number-of-characters/#findComment-522777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.