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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With