ngx-mask icon indicating copy to clipboard operation
ngx-mask copied to clipboard

How to mask optionally negative number?

Open dluk opened this issue 8 years ago • 11 comments

Hi,

How do I create a mask for optionally negative number? I have tried mask="-?0*.99" but it doesn't allow to enter anything after the -

dluk avatar Aug 28 '18 12:08 dluk

@dluk You should try to create custom pattern for this optional negative number like this:

<input type='text' [patterns]="customPatterns" mask="(000-000)" >

and in your component

public customPatterns = {'0': { pattern: new RegExp('[a-zA-Z]')}};

unnamed92 avatar Aug 28 '18 13:08 unnamed92

Hello I have used in the following way:

<input type="text" [patterns]="customPatterns" mask="999999.00" dropSpecialCharacters="false" >

component:

customPatterns = { '9': { pattern: new RegExp('[-?0-9]') } };

Now I can able to use - as optionally, but the issue is I couldn't enter like -12.23 and mask is forcing me to enter -123456.00 or 123456.00

is there any solution for the above issue?

lucky9abhi avatar Aug 29 '18 15:08 lucky9abhi

@lucky9abhi I think it's because you need to define patterns for all the symbols. You only defined '9' but not '0' so it treats the '0' exactly as '0' I guess something like this could work: <input type="text" [patterns]="customPatterns" mask="09999.99" dropSpecialCharacters="false" > with customPatterns = { '0': { pattern: new RegExp('-?') }, '9': { pattern: new RegExp('[0-9]') }, }; wondering if you could write mask like mask="09*.99" because I don't want to limit how big the number can be, though I want to have only 2 digits for fraction part. Anyway I gave up before trying this solution and moved on with another library. I don't think it should be as complex to make something as simple as a number with some fraction parts that can be negative. And it was simple with another library.

dluk avatar Aug 31 '18 11:08 dluk

@dluk You should try to create custom pattern, like this: in view: <input type="text" [patterns]="customPatterns" mask="V*.00" dropSpecialCharacters="false"; in component: custom patterns = 'V': { pattern: new RegExp('[-?0-9]') }; This custom pattern worked for me.

pryimakalex avatar Sep 04 '18 10:09 pryimakalex

@Alexandr98 But I think it would allow you to enter some wrong values like: "-.00", "9-.00", "-9-.00", "---.00"

dluk avatar Sep 04 '18 15:09 dluk

/^-?([0-9]+.)?[0-9]+$/g

vstets avatar Sep 11 '18 08:09 vstets

@dluk This should work: in view: <input type="text" mask="V0*.00" [patterns]="customPatterns" [dropSpecialCharacters]="false"> in component: custom patterns = 'V': { pattern: new RegExp('-?') };

pryimakalex avatar Oct 02 '18 08:10 pryimakalex

Why is this issue closed? Even using the @Alexandr98 solution, isn't working, because the "V" pattern, in practice, accepts all characters, not only "-". Please, fix this!

nicolasholanda avatar Aug 12 '19 14:08 nicolasholanda

This seems to work: customPatterns = { '0': { pattern: new RegExp('-|[0-9]') }, '9': { pattern: new RegExp('[0-9]') } };

<input mask="09*.99" [patterns]="customPatterns" [dropSpecialCharacters]="false">

mamontgo avatar Oct 17 '19 00:10 mamontgo

How I can see for angular 9+ there are added new parameter [allowNegativeNumbers]="true"

But for oldest version I think this should work. It works for me.

<input
  mask="separator.2"
  thousandSeparator=","
  [dropSpecialCharacters]="false">

[dropSpecialCharacters]="false"-> is required

AshotAleqs avatar Apr 22 '20 19:04 AshotAleqs

for me this work <input type="text"
mask="separator.2" [allowNegativeNumbers]="true" thousandSeparator="." separatorLimit="100000" formControlName="amount" />

Note: ngx-mask version 10.0.4 Angular 10+

daniellopezj avatar May 12 '21 15:05 daniellopezj