Javascript 正则表达式校验数字

时间:2023-03-08 22:13:16

$("input[datatype=number]").blur(function () {
                     var str = $(this).val();
                     if (!isDecimal(str)) {
                         alert("请输入数字");
                     }
                 });

function isDecimal(str) {
                if (isInteger(str)) return true;
                var re = /^[-]{0,1}(\d+)[\.]+(\d+)$/;
                if (re.test(str)) {
                    if (RegExp.$1 == 0 && RegExp.$2 == 0) return false;
                    return true;
                } else {
                    return false;
                }
            }
            function isInteger(str) {
                var regu = /^[-]{0,1}[0-9]{1,}$/;
                return regu.test(str);
            }