[CLOSED] Field value Numeric and allow 2 decimal

  1. #1

    [CLOSED] Field value Numeric and allow 2 decimal

    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?
    Last edited by Daniil; Nov 21, 2014 at 3:01 PM. Reason: [CLOSED]
  2. #2
    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 to see how the MaskRe works.
  3. #3
    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?
    Last edited by rafagracia; Jan 25, 2016 at 2:55 PM.
  4. #4
    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. Just swap commas for dots and the other way around.
    Last edited by fabricio.murta; Jan 25, 2016 at 6:48 PM.
    Fabrício Murta
    Developer & Support Expert
  5. #5
    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.
    Last edited by rafagracia; Jan 26, 2016 at 6:20 AM.

Similar Threads

  1. [CLOSED] Problems Decimal Numeric Field
    By opendat2000 in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: Apr 30, 2014, 2:02 PM
  2. [CLOSED] Numeric Field Validation Not working
    By PriceRightHTML5team in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: Apr 15, 2014, 8:40 AM
  3. [CLOSED] Numeric grid Filter, numeric field Decimal Places
    By RCM in forum 2.x Legacy Premium Help
    Replies: 6
    Last Post: Oct 10, 2013, 8:37 PM
  4. Replies: 1
    Last Post: Jul 12, 2011, 5:56 PM
  5. [CLOSED] Numeric Field MinValue
    By majestic in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Feb 09, 2010, 6:33 AM

Posting Permissions