Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxLength excluding sign and decimal symbol #608

Closed
nvkfr opened this issue Oct 9, 2017 · 3 comments
Closed

MaxLength excluding sign and decimal symbol #608

nvkfr opened this issue Oct 9, 2017 · 3 comments

Comments

@nvkfr
Copy link

nvkfr commented Oct 9, 2017

Hi @Mottie,

I'm looking to set a maxLength excluding the decimal symbol and the negative sign. An example of what I'm trying to do :

maxLength = 4;
input = -ii.dd => works

There are 6 characters but I need to have a way to only track numbers (in this case, 4 numbers). Is there currently a way to do this ?

On a side note, because I will probably be asked to do this, is there a way to limit the number of characters after the decimal symbol ?

Appreciate your help with this. Thanks in advance !

@Mottie
Copy link
Owner

Mottie commented Oct 9, 2017

Hi @nvkfr!

There are a bunch of demos on the Home wiki page, one of which is a demo named "Numpad limits to two decimal places"; but that doesn't account for a sign or integer part.

I set up this demo that should give you a general idea of how to implement it – it's not perfect and I'm not sure of your requirements.

$(function() {
  var maxInteger = 3,
    maxFractional = 2,
    regex = new RegExp(
      `([+-]?\\d{${maxInteger}}(?:\\.\\d{0,${maxFractional}})?)`
    );

  $("#keyboard").keyboard({
    layout: "custom",
    customLayout: {
      normal: ["7 8 9 {b}", "4 5 6 {clear}", "1 2 3 {c}", "0 {dec} {empty} {a}"]
    },
    restrictInput: true,
    change: function(e, keyboard, el) {
      var change,
        val = keyboard.$preview.val().replace(/[^\d-.]/g, ""),
        c = $.keyboard.caret(keyboard.$preview),
        start = c.start,
        end = c.end,
        restrict = val.match(regex);
      if (restrict) {
        restrict = restrict.slice(1).join("");
      } else {
        restrict = val;
      }
      keyboard.$preview.val(restrict);
      change = restrict.length - val.length;
      start += change;
      end += change;
      $.keyboard.caret(keyboard.$preview, start, end);
      // enable/disable decimal key as needed
      keyboard.checkDecimal();
    }
  });
});

@nvkfr
Copy link
Author

nvkfr commented Oct 10, 2017

Hi Mottie,

A huge thanks for your quick answer, will manage with this :)

Best regards,

@Mottie
Copy link
Owner

Mottie commented Nov 23, 2017

I'm guessing this issue has been resolved, so I'm going to close it. If you continue to have problems, please feel free to continue the discussion in this thread.

@Mottie Mottie closed this as completed Nov 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants