-1

I am trying not to allow asterisk character in my validation. My regex expression is

addressFormat="^[a-zA-Z0-9 \~\!\@\#\$\%\^\*\(\)_\'\-\+\=\{\}\[\]\|\:\;\,\.\?\/]{0,45}$"

As specified from the link, Link I tried adding [^\*] as below.

"^[a-zA-Z0-9 \~\!\@\#\$\%\^\*\(\)_\'\-\+\=\{\}\[\]\|\:\;\,\.\?\/][^\*]{0,45}$"

"^[^\*][a-zA-Z0-9 \~\!\@\#\$\%\^\*\(\)_\'\-\+\=\{\}\[\]\|\:\;\,\.\?\/]{0,45}$"

But it is allowing asterisk * character in my textbox. What is the mistake in my code. ? Any suggestions..

Community
  • 1
  • 1
kk1076
  • 1,740
  • 13
  • 45
  • 76
  • 1
    Why don't you remove the asterisk from your character class? – Casimir et Hippolyte May 26 '14 at 10:40
  • Why not just removing the asteriks from the allowed chars? (oops... as @CasimiretHippolyte said while I was writing ;-) ) – Jcl May 26 '14 at 10:40
  • Yes. I tried removing the asterisk character in the expression,. But still its allowing – kk1076 May 26 '14 at 10:43
  • Your regex will be more readable if you don't escape all characters, it is not mandatory within a character class. And as other said, remove the `*` from the character class. – Toto May 26 '14 at 10:48
  • You don't need escaping * inside []. [^*] will work just fine. As others pointed out, you should be able to not disallow asterisk, by just not specifiying it in your first part of regex. Or do you just want to disallow asterisk at the end of the string? – Edin May 26 '14 at 10:51
  • FYI, added explanation of why it didn't work and discussion of alternate option. – zx81 May 26 '14 at 11:05

3 Answers3

2

Your regex can be simplified to:

"^[a-zA-Z0-9 ~!@#$%^*()_'+={}\[\]|:;,.?/-]{0,45}$"

and, as [a-zA-Z0-9_] is the same as \w:

"^[\w~!@#$%^*()'+={}\[\]|:;,.?/-]{0,45}$"

then you could remove the *:

"^[\w~!@#$%^()'+={}\[\]|:;,.?/-]{0,45}$"
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Ah, we posted nearly the same simplification at the same time. However, IMO `\w` is not a great idea as he is using .NET and it will match Nepalese characters, Klingon digits etc. :) – zx81 May 26 '14 at 10:56
  • @zx81: Thanks for he info, I thought it depended on the local. – Toto May 26 '14 at 11:00
  • Yes, it depends on the locale in PCRE and many flavors... But not .NET, which assumes an utf-8 string (and makes `\w`, `\d`, `\s` act accordingly) unless you specify ECMAScript. – zx81 May 26 '14 at 11:05
2

First, for your information, you can simplify your regex to:

^(?i)[-a-z0-9 ~!@#$%^()_'+={}[\]|:;,.?/]{0,45}$

Since you are using C#, do not yield to the temptation of replacing [0-9a-z_] with \w unless you use the ECMAScript option, as C# assumes your strings are utf-8 by default, and \w will too happily match Arabic digits, Nepalese characters and so forth, which you might not want... Unless this is okay:

abcdᚠᚱᚩᚠᚢᚱტყაოსdᚉᚔమరמטᓂᕆᔭᕌसられま래도654۳۲١८৮੪૯୫୬१७੩௮௫౫೮൬൪๘໒໕២៧៦᠖

(But that's 60 chars, over your 45 limit anyway... Whew.)

More interestingly:

What was wrong before?

When you have a regex such as [^*][a-z] (simplifying your earlier expression), the [^*] matches exactly one character, then the [a-z] matches exactly one other character (the next one). They do not work together to impose a condition on the next character. Each of them are character classes, and each character specifies the next character to be matched, subject to an optional quantifier (in your case, the {0,45}

Would this work?

On the surface, this might look like the ticket, but I do not recommend it:

^[^*]{0,45}$

Why not? This matches any character that is not an asterisk, zero to 45 times. That sounds good, but eligible characters would include tabs, new lines, and any glyph in any language... Probably not what you are looking for.

zx81
  • 41,100
  • 9
  • 89
  • 105
1

Delete \* from your expression.
Also look at this link - it's really helpfull when you writing the regular expressions.

jsFiddle example

HTML

<form>
<input type="text" required pattern="^[a-zA-Z0-9 \~\!\@\#\$\%\^\(\)_\'\-\+\=\{\}\[\]\|\:\;\,\.\?\/]{0,45}$" title="incorrect format"/>
    <input type="submit"/>
</form>
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Nicolai
  • 1,907
  • 1
  • 23
  • 31