Search code examples
javascriptregexangularjsx-editable

Regular expression is not working on xeditable angular directive


I'm using xeditable angular directive. I need to use a regex in one of my fields to validate positive decimal values. However, it's not working properly. Could you tell me why? Thanks in advance.

Example values include 12,125.25, etc.

<span editable-text="user.name" e-form="tableform" e-pattern="/^\d+(\.\d{1,2})?$/">
                {{ user.name || 'empty' }}
 </span>

JSFiddle


Solution

  • You need to fix the regex as it does not accept thousand separator now.

    Use this regex:

    ^\d{1,3}(?:,\d{3})*(\.\d{1,2})?$
    

    See demo

    I added:

    • \d{1,3} - Match 1 to 3 digits
    • (?:,\d{3})* - Match 0 or more sequences of , + 3 digits.

    If non-capturing groups do not work for you, remove ?:.

    If you do not want to allow 0-only input (i.e. the number only consists of 0s), use

    ^(?![0,.]+$)\d{1,3}(?:,\d{3})*(\.\d{1,2})?$
    

    See demo