Skip to content
New issue

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

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

Already on GitHub? # to your account

How to restricist entry upto 2 decimal points only eg 9999.20,11.12,777854.65 #243

Closed
rohitbhatia2691 opened this issue Feb 26, 2014 · 3 comments

Comments

@rohitbhatia2691
Copy link

How to restrict entry upto 2 decimal points only eg
allowed entry 9999.20,11.12,777854.65,1.1
not allowed entry 444.123,548.25335 etc
we have restrict only two decimal place only i.e the precession is 2

@Mottie
Copy link
Owner

Mottie commented Feb 26, 2014

Hi @rohitbhatia2691!

Try this regex (demo):

$('#keyboard').keyboard({
    layout: 'num',
    restrictInput: true,
    change : function(e, keyboard, el) {
        var val = keyboard.$preview.val().match(/(\d+)(\.\d{0,2})?/);
        if (val) {
            keyboard.$preview.val( val.slice(1).join('') );
        }
    }
});

@rohitbhatia2691
Copy link
Author

thxs its work

@Mottie
Copy link
Owner

Mottie commented Feb 27, 2014

I updated the demo to remember the caret position, so the caret won't jump to the end once the value gets updated. This happens when you try to type numbers to the left of an existing decimal value (new demo):

$('#keyboard').keyboard({
    layout: 'num',
    restrictInput: true,
    change : function(e, keyboard, el) {
        var val = keyboard.$preview.val().match(/(\d+)(\.\d{0,2})?/);
        if (val) {
            keyboard.$preview
                .val(val.slice(1).join(''))
                .caret( keyboard.lastCaret.start, keyboard.lastCaret.end );
        }
    }
});

@Mottie Mottie closed this as completed Feb 27, 2014
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants