0

I'm creating a Log in form and registration if the user is new. I need to make sure when they are registering that they must include one of these characters $, #, @, !

The challenging part is how to compare the whole password text inserted by the user with every single symbol. I know how to compare it with the whole pattern but I couldn't figure out how to compare the password with one by one of the symbols. Also the username is better to keep it only characters and numbers, how do i strict out the rest of the symbols from it? And display an error message and redbox around the textboxes in the form saying that "Password must include $, #, @, !" and if the username error says "It must have only characters and digits"

Appreciate it.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • [see this](http://stackoverflow.com/questions/2637896/php-regular-expression-for-strong-password-validation), second answer is the way to go – Saqueib Feb 09 '15 at 03:08
  • Thank you it worked fine, i used preg_match –  Feb 09 '15 at 22:03

1 Answers1

0

I think you'd have to use JavaScript with the onblur (the moment when you go out of focus of the field) of your password activating a JavaScript function that verify if the password is valid and if it is not it changes the background color of the password field.

<input type="password" name="pass" onblur="verifyPass(this)"/>

function verifyPass(field) 
{
   if(condition on the field the text is obtained with field.value)
   {
      field.style.backgroundColor = "#fba";
      return false;
   }
   else
   {
      field.style.backgroundColor = "";
      return true;
   }
}
ralphS16
  • 3
  • 1
  • What about the regex part and how to validate if it has one of these symbols? –  Feb 09 '15 at 15:15
  • I am not familiar with regex but to verify the symbols you can parse the string and look for these symbols with an easy for loop (not a while because you would also need to check for invalid characters) I understand that it would be really long so maybe the regex would be easier to use and maybe it could be implemented in JavaScript. – ralphS16 Feb 09 '15 at 19:54