How to do alphanumeric password

Thanks for the advice… I am also wondering if this should work? Would a preg_match check if the word would consists of both numerical and alphabetical words if i do this?

if (!preg_match("/^[a-zA-Z0-9]*$/", $password)){
                   	   header("Location: ../signup.php?signup=invvalidalphanumeric");
                   	   exit();

if your ctype_alnum() doesn’t work, then your preg_match() won’t work either, since they do the same.

In hindsight, I think the preg_match should work because it did work when I was checking whether my first name and last name was empty or not but not for password…

Might it be that in your tests you are copying-pasting in white space? Without passing the input value through trim() if there is an “invisible” space or tab entered into the input, that could cause the conditional checks to fail.

So let’s see if I understand what ctype_alnum is about… if I add this code… I must type a password that contains both numbers and letters? I tried it again but it still excepts just words without numbers…and there are no errors displaying:


<?php


if(isset($_POST['submit'])) {
   include_once 'dbh.php';

   $username = $_POST['username'];
   $firstname = $_POST['firstname'];
   $lastname = $_POST['lastname'];
   $email = $_POST['email'];
   $password = $_POST['password'];

   // Throw error messages first and then success messages

   if(empty($username) || empty($firstname) || empty($lastname) || empty($email) || empty($password)) {
      header('Location: ../index6.php?signup=empty');
      exit(); // close off the script.... 
   }
   else {
      if(!preg_match("/^[a-zA-Z]*$/", $firstname) || !preg_match("/^[a-zA-Z]*$/", $Lastname)) {
      header('Location: ../index6.php?signup=char');
      exit();
      }

   else {
      if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      header("Location: ../index6.php?signup=email&firstname=$firstname&lastname=$lastname&username=$username");
      exit();
      }

      if(!ctype_alnum($password)) {
         header("Location: ../index6.php?signup=hastobealphanumeric");
         exit();
      } else {


      else {
      	header('Location: ../index6.php?signup=success');
      	exit();
      }
   
    }
   }
}
}
else {
	header('Location: ../index6.php');
}





I found this page but don’t really understand much about the code… Don’t know why this is working…

if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $password)) {
    echo 'the password does not meet the requirements!';
}

The password must contain either numbers or letters, not both numbers and letters, I believe. There is a difference.

Is this for the preg_match or ctype?

For ctype_alnum, for sure. Read the PHP manual (see post #20) “Returns TRUE if every character in text is either a letter or a digit, FALSE otherwise”. I am not that expert on regular expressions, but it looks like that is the case there also.

1 Like

Regex can be confusing at times. I find it helps to experiment with a regex tester like this one:-

It doesn’t just help you test, it can help you learn regex, by explaining what’s happening, plus the “Quick Reference” is a good cheat sheet.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.