How to mask optionally negative number?
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 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]')}};
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 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 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.
@Alexandr98 But I think it would allow you to enter some wrong values like: "-.00", "9-.00", "-9-.00", "---.00"
/^-?([0-9]+.)?[0-9]+$/g
@dluk This should work: in view: <input type="text" mask="V0*.00" [patterns]="customPatterns" [dropSpecialCharacters]="false"> in component: custom patterns = 'V': { pattern: new RegExp('-?') };
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!
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">
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
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+