AngularJS ng-pattern with or within textbox

2k Views Asked by At

I need to limit an input box to either accept a number or a certain string pattern

<input type="text" ng-model="value" name="value" ng-pattern="/^([0-9])|([R])$/" required>

This does not seem to fully work, i can add 555f and this will be a valid value for my ng-model

How can i limit this to either match a digit or my string pattern?

http://jsfiddle.net/DaleS/dv7vuecz/

2

There are 2 best solutions below

0
On

You have a problem with your reg exp in ng-pattern, this: /^([0-9])|([R])$/ will mach any string that starts from a number or ends with 'R'. And for what you want, you need smth like this:

/^[0-9]+$|^[R]+$/

It will match any string that is number, or starts and ends with 'R'.

0
On
^([0-9]|[R])$

Try this.See demo.

https://regex101.com/r/eZ0yP4/10