0

I have the following input field that accepts the price as a value.

input{ 
    type="text" 
    name="price" 
    np-currency=true
    ng-model='price'
    ng-pattern="/[$,.]\d*$/"
}

This pattern("/[$,.]\d*$/") makes the input field invalid for $25.34@@@C%34 which is correct however just 25 also becomes invalid thats because it does not have either $ or , or. How do I make it optional?

I need $25.00 or 25 as valid but not $25.00$%#$%%@@.

requirement: any digits $, , . are valid

I really appreciate any suggestions on this regular expression.

Ramya
  • 107
  • 1
  • 1
  • 12
  • Try `"/^[$]?\d*(?:\.\d+)?$/"` (if you also need to let the empty string as valid). Note that the requirements are not clear. – Wiktor Stribiżew Feb 05 '16 at 22:30
  • @WiktorStribiżew: Might wanna add `,` too. –  Feb 05 '16 at 22:34
  • Is `,` a digit grouping symbol (thousand separator)? Or is it Russian/Polish/etc. decimal separator? What about just `,,,,,,`? There are lots of questions I can ask looking at the current requirements. – Wiktor Stribiżew Feb 05 '16 at 22:36
  • It's USD and the max allowed value is 100,000 – Ramya Feb 05 '16 at 22:38
  • Why not just use `type="number"`, and assume the `$` as implicit? what other logic are you using in your app that you care if there is a `$` at the beginning or not, or how many separators there are? besides that, if you are going to do math using the value, then `text` doesn't make sense anyway. – Claies Feb 05 '16 at 22:40
  • Try `ng-pattern="/^[$]?(?:\d{0,3}(?:,\d{3})*|\d{4,})(?:\.\d+)?$/"` – Wiktor Stribiżew Feb 05 '16 at 22:43
  • well that did it partially, its still valid for `56.34$` which should be invalid. – Ramya Feb 05 '16 at 22:46
  • 1
    [My fiddle say it works ok](http://jsfiddle.net/xh70ffmy/2/). Can you share yours? – Wiktor Stribiżew Feb 05 '16 at 23:23

0 Answers0