简体   繁体   中英

How to allow alphanumeric and certain special characters in angularjs using regex

This question might be a simple one, but looks complex when trying to use regex pattern for this.

I have a textarea and I'm trying to allow alphanumeric characters and only 5 certain special characters like . , ' " : . , ' " :

Other special characters should be disabled when typing.

I have tried to use ng-pattern from angularjs, but when I start typing special characters other than allowed one, it is not disabled.

<textarea rows="5" cols="50" ng-pattern="/^[a-zA-Z0-9.,:'"]*$/">

The main problem is when I try to allow ' " both of these certain special characters, facing syntax issue in ng-pattern.

I'm expecting a simple solution using only regex in ng-pattern.

I'm missing somewhere, can someone help on this?

 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <textarea rows="5" cols="50" ng-pattern="/^[a-zA-Z0-9.,:'"]*$/"> </textarea> 

You need to html encode the double quote character inside the pattern, like this:

ng-pattern="/^[a-zA-Z0-9.,:&apos;&quot;]*$/"

Currently the pattern ends when it reaches the first double quote.

For safety, also html encode the single quote.

Edit , I had made a mistake, now it should Work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM