Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-pattern allows space when regex should prohibit it

ng-pattern is allowing spaces with the following pattern and I cannot understand why it would do so.

ng-pattern="/^[a-zA-Z\d\-\_]+$/"

I have tried without escaping the - and _, and the result is the same: the classes ng-valid and ng-valid-pattern are applied.

I am trying to only allow a-z (both capital and lower case) and dash and underscore. The pattern seems to work as I'd expect on regex101 so this is quite confusing.

UPDATE: the text field is marked as invalid after a valid character is typed after a space, but not when the space is the first character: eg (not really hardcoded like this, the "someText" and "some text" are typed into the text inputs):

<input> someText</input> //ng-valid
<input>some text</input> //ng-invalid

Still quite confused why this would be.

like image 553
1252748 Avatar asked Dec 04 '22 03:12

1252748


1 Answers

Angular trims inputs automatically. You can disable it by ng-trim="false"

<input type="text" ng-model="model" ng-trim="false" ng-pattern="/^[a-zA-Z\d\-\_]+$/"/>

see https://docs.angularjs.org/api/ng/input/input%5Btext%5D

like image 147
milanlempera Avatar answered Dec 06 '22 17:12

milanlempera