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.

2
  • 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. Commented Nov 14, 2016 at 8: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. Commented Nov 14, 2016 at 8:56

2 Answers 2

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.

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

2 Comments

Wiktor, How can I restrict the allowed characters to be maximum 32 characters in the above regular expressions?
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.
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.

7 Comments

Even though in your solution [\w~!@#\$-]+ there is no underscore, but still underscore is getting allowed, how come?
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...
Now if I need to accept all alpha numeric with no space and no underscore (_), what I have to do?
@MadasuK: You are contradicting yourself: But it should allow only the ~!@#$-_ along with alphanumerics. You stated you need to match _.
@WiktorStribiżew true, but for the sake of answering, you take away the \w and add A-Za-Z0-9 instead
|

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.