校验是否为一个数字,以及该数字小数点位数是否与参数floats一致

5年以前  |  阅读数:432 次  |  编程语言:JavaScript 

//校验规则:
//   若参数floats有值,则校验该数字小数点后的位数。
//   若参数floats没有值,则仅仅校验是否为数字。
function isNum(value,floats=null){
    let regexp = new RegExp(`^[1-9][0-9]*.[0-9]{${floats}}$|^0.[0-9]{${floats}}$`);
    return typeof value === 'number' && floats?regexp.test(String(value)):true;
}

function anysicIntLength(minLength,maxLength){
    let result_str = '';
    if(minLength){
        switch(maxLength){
            case undefined:
                result_str = result_str.concat(`{${minLength-1}}`);
                break;
            case null:
                result_str = result_str.concat(`{${minLength-1},}`);
                break;
            default:
                result_str = result_str.concat(`{${minLength-1},${maxLength-1}}`);
        }
    }else{
        result_str = result_str.concat('*');
    }

    return result_str;
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8