Help with Alphanumeric change password function

Hello Friends

I am new to PHP.
I was going through this below Change user password function and I am trying to make the Change password function to accept only alphanumeric values. Could you please help me make the below function alphanumeric.

//function to change the password
private function changepassUser() {
 
$strOldPasswd = ''; 
$arUserData['passwd']='';
if(isset($_REQUEST['passwd']) && isset($_REQUEST['newpasswd'])) {
   $strNewPasswd = $_REQUEST['newpasswd'] ;
  
   if ( strlen($strNewPasswd) <= 1){
			  $this->arErrors[] ="Password must be more than 3 char legth";
			  include("incs/changepasswd.php");
			  return false;
	}
    	
 	$strNewPasswd = SHA1(addslashes($_REQUEST['newpasswd']));
	$salt ="%#0974e411!&*$";
	$strOldPasswd =   $_REQUEST['passwd'] . $salt;
	$strOldPasswd =  SHA1($strOldPasswd);
	$sql = "SELECT passwd FROM users WHERE id = $this->intUserID"; 
	$arUserData = $this->objDB->getRow($sql);
	//echo "<p>old db: {$arUserData['passwd']}</p>"; 
	//echo "<p>old : $strOldPasswd</p>"; 
	
	if($arUserData['passwd'] == $strOldPasswd ){
     		
		$strPasswd = addslashes($_REQUEST['newpasswd']) ;
		$strPasswd =   $strPasswd . $salt;
	$strPasswd =  SHA1($strPasswd);
	
		$sql = "UPDATE users SET passwd = '$strPasswd' WHERE id = $this->intUserID";
		$this->objDB->update($sql);
       		echo"<h2>The new password was successfully updated.</h2>";
     }
     else {
	  $this->arErrors[] = "Please enter a valid passwd.";
	
}
}else {
    include("incs/changepasswd.php");
}

}

Thanks in advance.

  1. what error messages are you getting

  2. what is the script doing that it shouldn’t do or vice-versa?

If you have new password in variable $newPassword, then you could check like this:

if(preg_match('/^[a-z0-9]+$/i',$newPassword)>0){
//everything is great
}
else {
//Password contains nonalphanumeric characters
}

Hello Kalon

The above script is to change the user password and the new password should contain at least minimum 3 characters and the script is working fine. For the user to change the password there is a from where he should enter old password followed by the new password and hit update button. All this is working fine.
But the script is accepting only alphabets and I would want the script to force the user to enter numeric values along with the alphabetic values for his new password.

Thanks for the reply…

Thanks Aleksejs

Will try it now…