且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jquery和regex验证小数字段?

更新时间:2023-02-19 23:16:05

在@elclanrs和@mgibsonbr的帮助下,我来到了这里:

With the help of @elclanrs and @mgibsonbr, I came to this:

$j = jQuery.noConflict();
$j(function() {
    $j("input").keypress(function(e) {
        var val = $j(this).val();
        var regex = /^(\+|-)?(\d*\.?\d*)$/;
        if (regex.test(val + String.fromCharCode(e.charCode))) {
            return true;
        }
        return false;
    });
});

可在此处进行测试: http://jsfiddle.net/9U2Mw/5/