3

I am not seeing the ng-invalid class applied when I type into the field a string like abc. The ng-pattern works if i test it on the console...but not in angular.

<input type="number"
 step=".05"
 ng-pattern="/^\d{0,}\.{0,1}\d{0,}$/"
 ng-model="someNumber">
6
  • 1
    Seems to work for me: plnkr.co/edit/rxbmgUHPtNmmZPJsBAOJ?p=preview Which version of angular are you using? Commented Nov 23, 2013 at 8:34
  • This question seems similar, posted yesterday. No answers yet. Are you on Angular 1.0.7 as well? Commented Nov 23, 2013 at 9:30
  • i'm on 1.0.8 which I thought was the latest stable release. Commented Nov 23, 2013 at 9:31
  • @chovy The latest stable version is now 1.2 Commented Nov 23, 2013 at 9:48
  • still doesn't work for me. neither does your example (chrome) Commented Nov 23, 2013 at 10:03

1 Answer 1

2

@musically_ut's plnkr in the comments works for Firefox—i.e. the input element gets class ng-invalid for string abc—but not for Chrome.

For Chrome, it works for me if I add the required attribute, i.e.

<input type="number"
       step=".05"
       required
       ng-pattern="/^\d{0,}\.{0,1}\d{0,}$/"
       ng-model="someNumber">
Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to allow empty values?
Hmm, required means empty isn't valid, doesn't it? Well, I'm still in the midst of working on the same kind of issues, but so far I seem to be having the validation success you're looking for with changing the type from number to text. Only downside I see so far is losing the up/down spin buttons (ok for me). I'm still exploring whether I need to have pattern in addition to ng-pattern, but I'm hoping not as that causes the browser to show its own prompt like "Please match the requested format".
sure, required is ok but when the page loads its empty until the data gets filled in. so it shows as an error, which I want to avoid. Maybe I just make the regex match empty as well.

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.