-
Notifications
You must be signed in to change notification settings - Fork 723
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
Comments
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();
}
});
}); |
Hi Mottie, A huge thanks for your quick answer, will manage with this :) Best regards, |
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. |
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 !
The text was updated successfully, but these errors were encountered: