javascriptkendo-uikendo-maskedtextbox

Applying Regular Expressions in a Kendo MaskedTextBox


I want to globally assign a REGULAR EXPRESSION to a Kendo MaskedTextBox using JavaScript...but cannot get Kendo to 'recognize' the pattern(s).

...everything I have tried fails.

SAMPLE PATTERNS:
Some examples of things I might use include things like...

I want to do "something" like...

$('#txtMeterNumber').kendoMaskedTextBox({
    mask: "basicText",
    rules: {
        "basicText": /^[a-zA-Z0-9,.- ]*$/
    }
});

Solution

  • You should use only one character to define the expression inside the "rules" object. Each character inside the mask property represents a single character in the actual input.

    There are some examples in the docs.

    Probably this is what you want:

    $(document).ready(function(){
      $('#txtMeterNumber').kendoMaskedTextBox({
        mask: 'xxx-xxx-xxx',
        rules: {
          'x': /[a-zA-Z0-9- ]/
        }
      });
    });
    <script src="https://kendo.cdn.telerik.com/2014.2.716/js/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2014.2.716/js/kendo.ui.core.min.js"></script>
    <input id="txtMeterNumber">


    Basic Text: ^[a-zA-Z0-9,.- ]*$
    

    This expression is incorrect, you have to scape the -. I think [a-zA-Z0-9,.\- ] will do the trick. You can check it at https://regexr.com/