- Forums
- PHP
- PHP Tutorials - How Limit Maximum Characters Letters Numbers In A Web Form
this toturial guide offers you a way to limit the amount of characters in a string for example you can use this information when you have a form and you want to limit the amount of characters you are allowed to submit with your form in php [993], Last Updated: Sat May 18, 2024
webmaster
Wed Dec 06, 2006
0 Comments
2854 Visits
This will show you how To Find and Limit the amount of characters in a field from a web form a user is allowed to enter.
sometime is important to know how many characters a string has, for example, if you have a web form, and you want to limit the amount of letters, numbers or any character into a fiel you can do that with the PHP strlen() function.
In my example, i have a form and i only want to allow the user to enter upto 5 character in length. So if the user enters more than 5 letter for example, the form will prompt the user there is an error and to try again.
First, its important that you have php. If you don't have php, we recommend you buy a php plan from our friends at www.webune.com they have great PHP hosting plans for your domain name or website.
Ok, that's all you need - PHP hosting. Once you know you have php, you can continue, otherwise, if you don't, this script will not work unless the script is parsed by PHP engine.
1. the first step in this tutorial guide is to copy and past the sample code below to your favorite editor, I will be using notepad.exe in Windows for this simple tutorial. So, copy and past this code:
CODE:
<?php
###################################################
####### SCRIPT CREATED BY WALLPAPERAMA.COM #####
###################################################
$leghth_limit="5";
# If the form has been sumbited, use the default message
if($_POST['name'])
{
# checks if the name has more than 10 characters
if (strlen($_POST['name']) > $leghth_limit)
{
$message='<span class="style2"><strong>ERROR: only '.$leghth_limit.' Characters are allowed, try again</strong></span>';
}
else
{
$message='<span class="style3"><strong>Congratulations! The form has been submitted.</strong></span>';
}
}
else
{
$message='Please Enter Your Name (Max '.$leghth_limit.' Characters)';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Wallpaperama.com Tutorial - How To Limit Characters in a Word</title>
<style type="text/css">
<!--
.style2 {
color: #FF0000;
font-weight: bold;
}
.style3 {
color: #009900;
font-weight: bold;
}
-->
</style>
</head>
<body>
<div align="center">
<h1>How To Limit Characters in a Word</h1>
</div>
<div align="center">
<p>by <a href="http://www.wallpaperama.com">Wallpaperama.com</a></p>
<p align="left"> </p>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<table width="500" border="0" align="center" cellpadding="10" cellspacing="1" bgcolor="#CCCCCC">
<tr align="center" bgcolor="#FFFFFF">
<td colspan="2" nowrap><?php echo $message; ?></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="right">Name: </td>
<td><input type="textfield" name="name" ></td>
</tr>
<tr bgcolor="#FFFFFF">
<td> </td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
<p align="center">NOTE: If you enter more than <?php echo $leghth_limit; ?> character, the form will fail </p>
<p align="left"> </p>
<p align="left"><a href="http://www.wallpaperama.com/forums/"><< Go back to forum</a></p>
<p align="left">If you have found this simple and short tutorial helpful, we would like to ask you to please link to our website at http://www.wallpaperama.com by linking to Wallpaperama you will help us make tutorials like this one more easier to find by others who are looking for answers like this one.</p>
<hr>
<p align="center">Free Hosting for this PHP script Provided By <a href="http://www.webune.com">Webune.com</a></p>
</div>
</body>
</html>
2. Now save this file as "limit-strings.php", if you are using notepad, make sure to put the quotes ( " ) when Saving As
3. Now upload the limit-strings.php file to your hosting space on your website which has PHP. This will show you how the
strlen() function works so you can now start using it for your projects.
4. if you want to see this script in actions, our friends at www.webune.com have provided us with free PHP space for the purpose of this tutorial.
If you have found this simple and short tutorial helpful, we would like to ask you to please link to our website at http://www.wallpaperama.com by linking to Wallpaperama you will help us make tutorials like this one more easier to find by others who are looking for answers like this one.
Thank You
Wallpaperama Team.