Allow numbers only for input

I; m is trying to implement a javacript function that will not allow the user to enter anything but floating point numbers (digits)

This is my approach, but I do not know how to improve it, so that you can also specify the number of negative numbers (allow the "key") and work with IE.

function digits_only(evt, form) {
    var evt = evt || window.event,
        targ = evt.target || evt.srcElement,
        charCode = evt.which || evt.keyCode,
        keyChar = String.fromCharCode(charCode),
        isValid = true;
    if (charCode > 13) {
        isValid = /[0-9.]/.test(keyChar);
        //if a dolt is already in input
        if (keyChar === '.' && /\./.test(targ.value)) {
            isValid = false;
        }
    }
    return isValid;
}
+1
source share
3 answers

I think you are looking for what are called input masks. They are much more powerful than just numbers. But you can use them for this. "Google javascript input mask" or select this jQuery plugin .

EDIT: , , ...

+3

- ASP.NET? AjaxControlToolkits MaskEditExtender.

+1

I ask a similar question here:
restrict user input using javascript

but no one gives an exact answer to the problem, so wait for WebForms 2.0

0
source

All Articles