4

In my Angularjs application I need to have regular expression for two patterns for form validation with the following condition.

Pattern 1 :

The input box should accept alphanumeric with no space and it should also allow the user to use the characters like ~!@#$-_ any where in the string, except these characters non of the other characters should be allowed like (%, &, ^ etc). It should not allow leading/trailing whitespace also.

Examples :

 ab@4_w    :  valid
 sd!tye123  : valid
 sd%tye123  :  Invalid
 sd*tye123  :  Invalid

$scope.pattern1 = [\w~!@#\$-]+

Pattern 2: Should allow only alphanumeric with no space and no other characters including (_). It should not allow leading/trailing whitespace also.

Examples :

  a4hgg5  : Valid
  a4_6hy   : Invalid
  a@yb    : invalid

$scope.pattern2 = [\w]+

$scope.pattern1 and $scope.pattern2 needs to be modified to meet my above requirements.

Madasu K
  • 1,813
  • 2
  • 38
  • 72
  • Can you please precise the requirements? Can there be leading/trailing whitespace? Should a dot be allowed and where? Are `~!@#$-_` allowed anywhere in the string? If possible, please provide a fiddle to test. – Wiktor Stribiżew Nov 14 '16 at 08:21
  • Please clarify the requirements. You say you need `_` in the question, but, in the comment, you say you do not want to match `_`. A JS fiddle would be of great help. – Wiktor Stribiżew Nov 14 '16 at 08:56

2 Answers2

1

Try

^[\w~!@#\$-]+$

Explanation

  1. ^ Begin of string
  2. [\w~!@#\$-]+ Any number of the Characters you want, (note the need to escape $ like \$)
  3. $End of string

See in Action on Regex101. If you want empty strings to be valid, specify * instead of + for the quantifier. Also, when using \w you do not need to set the i flag, since it already covers both upper and lowercase letters.

nozzleman
  • 9,529
  • 4
  • 37
  • 58
  • Even though in your solution [\w~!@#\$-]+ there is no underscore, but still underscore is getting allowed, how come? – Madasu K Nov 14 '16 at 08:36
  • Thats because `\w` is equivalent to `[A-Za-Z0-9_]`, funny I know, i also dont know who specified this as an "word-character", but thats how it is... – nozzleman Nov 14 '16 at 08:40
  • Now if I need to accept all alpha numeric with no space and no underscore (_), what I have to do? – Madasu K Nov 14 '16 at 08:45
  • @MadasuK: You are contradicting yourself: *But it should allow only the `~!@#$-_` along with alphanumerics.* You stated you need to match `_`. – Wiktor Stribiżew Nov 14 '16 at 08:49
  • @WiktorStribiżew true, but for the sake of answering, you take away the `\w` and add `A-Za-Z0-9` instead – nozzleman Nov 14 '16 at 09:07
  • ok that solves the issue. Can you please restrict leading and ending white space also for your solution [\w~!@#\$-]+ – Madasu K Nov 14 '16 at 09:16
  • what do you mean by **resrict**, those should already be forbidden – nozzleman Nov 14 '16 at 09:17
1

It should not allow leading/trailing whitespace.

In both cases, add the ng-trim="false" attribute to the input element.

The input box should accept alphanumeric with no space and it should also allow the user to use the characters like ~!@#$-_ any where in the string, except these characters non of the other characters should be allowed like (%, &, ^ etc).

The pattern you have is correct, but escaping characters that do not have to be escaped is not recommended, use:

^[\w~!@#$-]+$

Where \w matches [a-zA-Z0-9_].

NOTE: if you pass the pattern as a string, do not add ^ and $ anchors, but double the backslashes: "[\\w~!@#$-]+".

Should allow only alphanumeric with no space and no other characters including (_).

It is much easier: ^[a-zA-Z]+$. Same comment about anchors as above applies.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Wiktor, How can I restrict the allowed characters to be maximum 32 characters in the above regular expressions? – Madasu K Nov 16 '16 at 05:14
  • Use a limiting quantifier: `^[\w~!@#$-]{1,32}$` - `{1,32}` means *repeat 1 to 32 times*. To allow an empty string, use `{0,32}`. No spaces in between. – Wiktor Stribiżew Nov 16 '16 at 07:10