1

I am to validate against following conditions:

  1. It would allow all the special characters
  2. It must contain at least 1 alphabet or alphanumeric
  3. only special character will not work

I used

/^\d*[a-zA-Z][a-zA-Z0-9]+/

It is working fine for all scenario but it does expect 2 characters or numbers or combination of two before entering special character.

Examples:

  1. a!!@#$*()_ fails but should pass

  2. a1!@#$*()_ passes

Please help correcting the regex.

5
  • 1
    Your example values are exactly the same, still one passes and the other fails? Commented Mar 25, 2013 at 14:23
  • Your 2nd example simply does not fail because you forgot the $ at the end of the regex to indicate and EOL and "a1" matches [a-zA-Z][a-zA-Z0-9]+. Commented Mar 25, 2013 at 14:27
  • [a-zA-Z][a-zA-Z0-9]+ how it will pass special characters? Commented Mar 25, 2013 at 14:30
  • What do you mean by "special characters"? For example, is π "special"? Commented Mar 25, 2013 at 14:39
  • no.Like !@#$%^&*()_+}{ Commented Mar 25, 2013 at 14:44

3 Answers 3

1

tl;dnr answer:

/^(?=[!-~]+$)(?=.*[a-zA-Z0-9])/

Explanation:

A common way to express "AND" conditions in regular expressions is like the following:

/
    ^
    (?= pattern 1)
    (?= pattern 2)
    (?= pattern 3)
    etc
/

Basically, we accept the start of input (^) only if the rest matches all given patterns. Otherwise, the whole expression fails.

In your example, the pattern 1 is to match everything, including "special characters". From your comment, you're looking for the printable ASCII range. An ASCII char is [!-~] and since we don't want anything else (like "international chars") and don't allow empty strings, the final pattern is:

    [!-~]+$

The second pattern is "at least one alphanumeric", which can be rephrased as "any amount of anything else, then one digit or letter", that is,

    .*[A-Za-z0-9]

Putting it all together:

    /^(?=[!-~]+$)(?=.*[a-zA-Z0-9])/

Some testing:

    re = /^(?=[!-~]+$)(?=.*[a-zA-Z0-9])/

    console.log(re.test("hi"))            // true
    console.log(re.test("a!!@#$*()_"))    // true

    console.log(re.test("~!!@#$*()_"))    // false
    console.log(re.test("π+1"))           // false
    console.log(re.test(""))              // false

Seems to work!

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

Comments

0

Your regex requires an alphabet character followed by an alphanumeric character. Clearly the first example fails that condition, while the second passes.

Your conditions have huge overlap, also. Saying it must contain "at least one alphabet or alphanumeric character" is the same as saying simply it must contain "at least one alphanumeric character". This condition also precludes the need to say the string cannot all be special characters; although there, you may have just been clarifying. The regex you need is very simple:

/[a-zA-Z0-9]/.test(str)

It searches anywhere in str for an alphanumeric character.

3 Comments

But how it would all special characters?
Why don't you try it? The above regex is not "anchored," i.e. there is no ^ or $ binding the match to the start or end of the string. Therefore, all other characters are ignored. The only thing that matters is that somewhere in the string, there is an alphanumeric character.
If you want to constrain the string to a certain set of special characters, then you can do this, ^(?=.*[a-zA-Z0-9])[a-zA-Z0-9!@#$%^&*]+$, but I truly think that you should try to understand the original regex provided before blindly going with this on (just because it might look more like what you're expecting).
0
  1. It would allow all the special characters => no test needed, any string matches this rule
  2. It must contain at least 1 alphabet or alphanumeric => /[a-zA-Z0-9]/
  3. only special character will not work => this is already addressed by the second condition.

Your regex:

/[a-zA-Z0-9]/.test(string)

[Edit] this is actually the same answer as @acheong87...

3 Comments

Could be simplified with \w.
@Simon no, \w also includes underscore
Ok, could be simplified to [^\W_] ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.