14

I am using a javascript validator which will let me build custom validation based on regexp

From their website: regexp=^[A-Za-z]{1,20}$ allow up to 20 alphabetic characters.

This will return an error if the entered data in the input field is outside this scope.

What I need is the string that will trigger an error for the inputfield if the value has an asterix as the first character.

I can make it trigger the opposite (an error if the first character is NOT an asterix) with:

regexp=[\u002A]

Heeeeelp please :-D

Levi Botelho
  • 22,763
  • 5
  • 51
  • 94
Splynx
  • 683
  • 1
  • 7
  • 20

3 Answers3

31

How about:

^[^\*]

Which matches any input that does not start with an asterisk; judging from the example regex, any input which does not match the regex will be cause a validation error, so with the double negative you should get the behaviour you want :-)

Explanation of my regex:

  • The first ^ means "at the start of the string"
  • The [ ... ] construct is a character class, which matches a single character among the ones enclosed within the brackets
  • The ^ in the beginning of the character class means "negate the character class", i.e. match any character that's not one of the ones listed
  • The \* means a literal *; * has a special meaning in regular expressions, so I've escaped it with a backslash. As Rob has pointed out in the comments, it's not strictly necessary to escape (most) special characters within a character class
Cameron
  • 86,330
  • 19
  • 177
  • 216
  • 2
    you don't need to escape the * inside [] – Rob Agar Mar 14 '11 at 01:38
  • @Rob: You're probably right. I've found that it depends on the platform, but I've never had any problems explicitly escaping using a backslash – Cameron Mar 14 '11 at 01:41
  • fair enough, I was only being pedantic because you got your answer in first :P – Rob Agar Mar 14 '11 at 01:52
  • @Rob: [FGIW](http://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem) syndrome, sorry ;-) In any case, it's good for others to know that escaping inside a character class is usually unnecessary – Cameron Mar 14 '11 at 02:00
  • Actually this will trigger an validation error if there is an asterix anywhere in the input value, not only if the first character is an asterix... – Splynx Mar 14 '11 at 02:06
  • @Splynx: Are you sure you included the first `^` (which anchors the rest of the regex to the beginning of the string)? The regex should only ever match a non-empty input with the *first* character not being a `*` – Cameron Mar 14 '11 at 02:19
  • My bad, it did in fact only check on the first character it seems - any ways it validates just like it should - thank you again! – Splynx Mar 14 '11 at 04:23
  • 1
    @Splynx: No problem ;-) By the way, if you need it to pass an empty input as valid too, you can use: `^(?:[^\*]|$)` – Cameron Mar 14 '11 at 04:27
  • 1
    Thank you for explaining the regex! – Aaronius Jun 02 '15 at 19:21
3

How about ^[^\*].+.

Broken down:

  • ^ = start of string.
  • [^\*] = any one character not the '*'.
  • .+ = any other character at least once.
DuckMaestro
  • 13,332
  • 9
  • 61
  • 83
0

You can invert character class by using ^ after [

regexp=[^\u002A]

Kamil Szot
  • 15,477
  • 6
  • 53
  • 62