JavaScript Decimal Place Restriction With RegEx

前端 未结 7 816
北恋
北恋 2020-12-06 12:23

I have some client-side validation against a text box, which only allows numbers up to two decimal places with no other input.

This script was a basis for entering n

相关标签:
7条回答
  • 2020-12-06 13:13

    You could use a javascript function instead of regex

    function IsValid(value) {
        var split = value.split('.');
    
        if (split.length != 2) {
            return false;
        }
        else if (split[1].length > 2 || !Number(split[1])) {
            return false;
        }
        else if (!(split[0] == '' || split[0] == '0')) {
            return false;
        }
    
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题