PDA

View Full Version : [CLOSED] Field value Numeric and allow 2 decimal



matrixwebtech
Nov 15, 2014, 7:33 AM
Hi
I set up a textfield to accept numeric value upto 2 decimal .



X.TextField()
.ID("txtTaxtotal")
.MaskRe(@"/[\d\.]/")
.Regex(@"/^\d+(\.\d{1,2})?$/")
with this setup textfield allow values more than 2 decimal and show a error validation message.how can i restrict input more the 2 decimal?

Daniil
Nov 15, 2014, 7:46 AM
Hi @matrixwebtech,

.MaskRe restricts a key input only. So, it checks each keystroke, but it doesn't check the entire value.

.Regex is for validation only, it doesn't cancel a keystroke.

I can suggest you to use a InputMask plugin or monitor keystrokes manually listening to a TextField's KeyPress event. You can investigate a TextField's filterKeys method (http://docs.sencha.com/extjs/4.2.1/source/Text2.html#Ext-form-field-Text-method-filterKeys) to see how the MaskRe works.

rafagracia
Jan 25, 2016, 12:20 PM
I need this, but I can't find the right input mask that optionally allows until two decimals. The number of natural numbers could be anything.

My cases:
54 -> true
10.234,01 -> true
5,2 -> true
1,23 -> true
155,89 -> true
12,258 -> false
1,2,5 -> false
sdf123 -> false

This is what I've tried so far (C# code):


X.NumberFieldFor(m => m.MyPoperty).ID("MyId").HideTrigger(true).MaskRe(@"/[\d,]/")
.Plugins(X.InputMask().ClearWhenInvalid(false)
.Mask("9?999,99")
);


X.NumberFieldFor(m => m.MyPoperty).ID("MyId").HideTrigger(true).MaskRe(@"/[\d,]/")
.Plugins(X.InputMask().ClearWhenInvalid(false)
.Mask("x,tt")
.MaskSymbols(symbols =>
{
symbols.Add(new MaskSymbol() { Name = "x", Regex = @"/^[0-9]*$/" });
symbols.Add(new MaskSymbol() { Name = "t", Regex = "[0-9]" });
})
);



X.NumberFieldFor(m => m.SuperficieRealSol).ID("MyId").HideTrigger(true)
.Plugins(X.InputMask().ClearWhenInvalid(false)
.Mask("x")
.MaskSymbols(symbols =>
{
symbols.Add(new MaskSymbol() { Name = "x", Regex = @"^\d+(\.\d{1,2})?$" });
})
);


X.NumberFieldFor(m => m.MyPoperty).ID("MyId")
.AllowExponential(false)
.DecimalPrecision(2)
.MinValue(0)
.HideTrigger(true);

Can you post the solution that you found please?

fabricio.murta
Jan 25, 2016, 7:43 PM
Hello!

About the regular expression, maybe you can use this one:



^(([0-9]{1-2}.){0-1}([0-9]{3}.)*|[0-9]+)(,[0-9]{1,2}){0,1}$


This would:
1. Just an arbitrary amount of numbers
0 - 1 - 239 - 892

2. (1) + followed by a comma and one to two digits
0,1 - 0,11 - 239,89

3. a beginning 1-2 digits followed by a dot and/or an arbitrary number of 3-digits followed by a dot and (2):
1.223 - 1.000,99 - 23.293,8 - 23.345.342,00 - 123.345.291.293.238,30

Let us know if the regexp does not work for you. It might need one or another modification if it doesn't.

This stackoverflow question also gives good alternatives: Regex for number with decimals and thousand separator (http://stackoverflow.com/questions/16148034). Just swap commas for dots and the other way around.

rafagracia
Jan 26, 2016, 7:14 AM
The problem is not the regex is how I have to match the regex with the mask symbols.
I'm not trying to validate the input, I'm trying to avoid that the user input more than two decimals. The idea is that if they type more than two decimal numbers they won't see these numbers in the numberfield, that's why I'm using inputmask.