Check number of characters
You want to ensure that a use has entered a password with the correct amount of characters , how would you go abou this task . Well with regular expressions this is easy. This example ensures that between 6 and 12 characters are entered , of course you can change this by altering the code . To test this out change the amount of characters entered in $str1 , have less than 6 , more than 12 , have the correct amount etc.
Code
<?php
//check the amount of characters in a string function
//Beginners PHP 2001
// http://www.beginnersphp.co.uk
Function CheckNoChars($strText)
{
//check for between 6 and 12 characters
if (eregi("^.{6,12}$" , $strText))
return true;
else
return false;
}
?>
<?php
//test the function
$str1 = "mypasswordistoolong";
if (CheckNoChars($str1))
//if its OK display this message
echo "this has the correct number of characters";
//if its not OK display this one instead
else
echo "incorrect number of characters";
?>
Example:
incorrect number of characters