7

I want to use a regex to limit the characters allowed. That is:

a - z /* a to z */
A - Z /* A to Z */
0 - 9 /* 0 to 9 */
_ - /* underscore & dash */
~ ! @ # $% ^ & * () /* allowed special characters */

and this is my regex function:

function validChr($str) {
    return preg_match('/^[A-Za-z0-9_~\-!@#\$%\^&*\(\)]+$/',$str);
}

I've actually tried it and the result as I want, but I still was not sure. Is my regex is correct? Or are there other forms regex? Please help as I am still new about this regex. Thank you.

4
  • 3
    It's OK, but you should add an escape symbol for * as well, since it's a wildcard (in your code it means 'no or any amount of & sign in string'). Commented Nov 29, 2012 at 8:04
  • @RodionBaskakov If * it's between square brackets, I believe you don't have to escape it. Commented Nov 29, 2012 at 8:08
  • @RodionBaskakov: to be like this preg_match('/^[A-Za-z0-9_~\-!@#\$%\^&\*\(\)]+$/',$str); ? Commented Nov 29, 2012 at 8:08
  • @Fredy regexr.com ;) Commented Nov 29, 2012 at 8:34

2 Answers 2

9

It works as it should.

You should only add \ before * to escape it.

Check it out here: Regular expression test

Sign up to request clarification or add additional context in comments.

1 Comment

yes..but no need to escape * if it's within a character class
1

You can use this function I made sometime ago for passwords. You can use it for any string by modifying the if coniditions. Put each special characters with a \ before. It also has a check for string to be 8-20 characters long

    function isPasswordValid($password){
            $whiteListed = "\$\@\#\^\|\!\~\=\+\-\_\.";
            $status = false;
            $message = "Password is invalid";
            $containsLetter  = preg_match('/[a-zA-Z]/', $password);
            $containsDigit   = preg_match('/\d/', $password);
            $containsSpecial = preg_match('/['.$whiteListed.']/', $password);
            $containsAnyOther = preg_match('/[^A-Za-z-\d'.$whiteListed.']/', $password);
            if (strlen($password) < 8 ) $message = "Password should be at least 8 characters long";
            else if (strlen($password) > 20 ) $message = "Password should be at maximum 20 characters long";
            else if(!$containsLetter) $message = "Password should contain at least one letter.";
            else if(!$containsDigit) $message = "Password should contain at least one number.";
            else if(!$containsSpecial) $message = "Password should contain at least one of these ".stripslashes( $whiteListed )." ";
            else if($containsAnyOther) $message = "Password should contain only the mentioned characters";
            else {
                $status = true;
                $message = "Password is valid";
            }
            return array(
                "status" => $status,
                "message" => $message
            );
    }

Output

$password = "asdasdasd"
print_r(isPasswordValid($password));
// [
//   "status"=>false,
//   "message" => "Password should contain at least one number."
//]

$password = "asdasd1$asd"
print_r(isPasswordValid($password));
// [
//   "status"=>true,
//   "message" => "Password is valid."
//]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.