views:

1284

answers:

1

Hi What's the best method to mask an input field to only allow float/double, without any jquery plugin.

Acutally i'm doing it like this:

$("#defaultvalue").bind("keypress", function(e) {
                if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                    return false;
                }
            });

but thats only for numbers thx

+1  A: 

JavaScripts parseFloat() function returns NaN, when your input is not a number. For i18n you will probably have to replace the floating point separator (e.g. , with .)

Daff
parseFloat truncates at the first non-digit. parseFloat('1.23ABC') == 1.23.
Greg
Oh you're right. Didn't test that... maybe a combination with Regex and parseFloat makes more sense then.
Daff
yeah, but how to use the regex correctly? i didn't get it running..
k0ni