PHP - field minimum - Asked By Olusegun A. on 28-Oct-09 10:52 AM

Hello there,

how do i code my password field to take minimum of 6 characters.
secondly, is it possible to code MT_RAND to generate a particular maximum numbers of characters for my confirmation code.

regards,
Olusegun

Use the StrLen function. - [)ia6l0 iii replied to Olusegun A. on 28-Oct-09 01:20 PM

You can use the strlen function to determine the length of the password variable and then print the message accordingly. 

Let's say your password variable is called $pwd

$passwordlength = strlen($pwd);

if ($passwordlength < 6) 
{
    print "too less"
}

And on the second question, yes you can generate passwords of specific length. For e.g. in the following example, $length specified, would prepare the $password of specified length.

$enc = sh1(mt_rand().mt_rand().mt_rand());
$password = sub_str($enc, 1, $length);

Also, could write a function that takes in the length and allowable characters, to generate a password of those characters. 

function generatePassword($maxlength, $allowablechars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
    $string = '';
    for ($counter = 0; $counter  < $maxlength; $counter ++)
    {
        $pos = rand(0, strlen($allowablechars)-1);
        $string .= $allowablechars{$pos};
    }
    return $string;
}

javascript regular expressions - Venkat K replied to Olusegun A. on 28-Oct-09 01:24 PM

For accepting 6 digits you can use the following regular expressions:

^\d{6}$

you can check the below link how you can create regular expressions:

http://eggheadcafe.com/forumpostsubmission.aspx?topicid=79&forumpostid=10125592

Thanks,