Regex currency validation

前端 未结 2 562
予麋鹿
予麋鹿 2020-11-28 07:30

I need Help for currency regex in jQuery function.

  • It optionally allows \"$\" sign only one time in beginning.
  • It allows comma as digital-group-separ
2条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 08:16

    Here is the regular expression that should achieve this for you.

    The start must be numeric or $ sign. There can be any number of digits with commas, but it must start and end with a digit. There can optionally be a decimal point with up to two digits after it at the end of the line.

    var your_input = "$1,000,000.00";
    var valid_dollar_amt_regex = /^\$?[0-9][0-9,]*[0-9]\.?[0-9]{0,2}$/i;
    
    if(valid_dollar_amt_regex.test(your_input))
        alert("Valid!");
    

    Or use this function

    function validate_money(i) {
        var valid_dollar_amt_regex = /^\$?[0-9][0-9,]*[0-9]\.?[0-9]{0,2}$/i;
        return valid_dollar_amt_regex.test(i);
    }
    

    See it working: http://jsfiddle.net/znuJf/

提交回复
热议问题