Masking a text field to accept decimals only

I use the technique from this link to mask my text box to accept strings that are in decimal format (single period digits).

How to define textbox input restrictions?

Here is the regular expression that I put in the mask:

b:Masking.Mask="^\d+(\.\d{1,2})?$"

For some odd reason, it allows you to enter numbers, but I can’t insert this period in the text box.

I also checked the regex, so the regex is definitely correct.

http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

What could be the problem?

0
source share
3 answers

:

^\d+([\.\d].{1,2})?$

DEMO

EDIT:

123..1 1 . :

^(\d+)?+([\.]{1})?+([\d]{1,2})?$

DEMO

+8

^(\d+)?+([\.]{1})?+([\d]{1,2})?$

   bool blHasDot = false;
   private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
        {
            // Allow Digits and BackSpace char
        }
        else if (e.KeyChar == '.' && !blHasDot)
        {
            //Allows only one Dot Char
            blHasDot=true;
        }
        else
        {
            e.Handled = true;
        }
    }
+1

, , JQuery

//define Decimal numeric restriction
jQuery.fn.ForceDecimalNumericRules = function () {
    return this.each(function () {
        $(this).keydown(function (event) {

            // Prevent shift key since its not needed
            if (event.shiftKey == true) {
                return false;
            }


            //backspace, tab,End,Home, left arrow, right arrow, delete
            if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 35 || event.keyCode == 36 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46) {
            }
            // Allow Only: keyboard 0-9, numpad 0-9,decimal point
            //TODO: To include validation for number of decimal places
            else if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 110) {
                // Allow normal operation             
            } else {
                // Prevent the rest                
                return false;
            }
        });
    });
}

:

$('# txtbox'). ForceDecimalNumericRules()

-2

Source: https://habr.com/ru/post/1711524/


All Articles