-3

i just try like this.

for($i=0;$i<$length;i++)
{
    if(!ctype_alpha($password[$i])
    {
        header("location:regist.php?err=Password not contain letter");

    }
    else if(!is_numeric($password[$i]))
    {
        header("location:regist.php?err=Password not contain  numbers");
    }
    else
    {
        //??
    }
}

how to validate password if the password must alphanumeric without using regular expression ?

user3551629
  • 9
  • 1
  • 6

2 Answers2

0

I think you are looking for ctype_alnum Check- http://www.php.net/manual/en/function.ctype-alnum.php

UPDATE-

You can check for special characters in your string by something like-

<?php
$test = "password123";

$special_char = "#$%^&*()+=-[]';,./{}|:<>?~";

if (false === strpbrk($test, $special_char))
{

    echo "Good Password";
}
?>
Vinit
  • 1,815
  • 17
  • 38
  • There is a huge difference between a white-list and a black-list. There is no way you can reliably get all non-alphanumeric characters in your black-list. Your original answer was a lot better :-) – jeroen May 31 '14 at 02:12
  • i have use ctype_alnum but when the pass is 'test' only string, the pass is true. is it right ? – user3551629 May 31 '14 at 02:37
0

( may be this is ugly coding) but this is what you need

$str="123adsd##$";

$error ='string should be alphanumeric';

$str1 = str_replace(range(0,9), '', $str);

$alph = array_merge(range('A', 'Z'), range('a', 'z'));

if(strlen($str1)>strlen($str) || strlen($str1)<strlen($str))
{
    $str2 = str_replace($alph, '', $str1);

if(strlen($str2)>strlen($str1) || strlen($str2)<strlen($str1))
{
  if(strlen($str2) >0)
  {
    echo $error;
  }
}
else{
    echo $error;
}
}
else{
    echo $error;
}

output

string should be alphanumeric
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35