Currency validation

前端 未结 5 1664
滥情空心
滥情空心 2020-12-14 16:11

Please help with me writing a JavaScript Validation for currency/money field.

So please provide any regular expressions if u have :)

相关标签:
5条回答
  • 2020-12-14 16:47

    For me its working fine for Indian currency in INR

    var regex = /^[1-9]{0,2}(,{0,1})(\d{2},)*(\d{3})*(?:\.\d{0,2})$/;
    var a = '1,111.11';
    regex.test(a); 
    
    0 讨论(0)
  • 2020-12-14 16:51

    You could use a regexp:

    var regex  = /^\d+(?:\.\d{0,2})$/;
    var numStr = "123.20";
    if (regex.test(numStr))
        alert("Number is valid");
    

    If you're not looking to be as strict with the decimal places you might find it easier to use the unary (+) operator to cast to a number to check it's validity:

    var numStr = "123.20";
    var numNum = +numStr; // gives 123.20
    

    If the number string is invalid, it will return NaN (Not a Number), something you can test for easily:

    var numStr = "ab123c";
    var numNum = +numStr;
    if (isNaN(numNum))
        alert("numNum is not a number");
    

    It will, of course, allow a user to add more decimal places but you can chop any extra off using number.toFixed(2) to round to 2 decimal places. parseFloat is much less strict with input and will pluck the first number it can find out of a string, as long as that string starts with a number, eg. parseFloat("123abc") would yield 123.

    0 讨论(0)
  • 2020-12-14 17:00
    /[1-9]\d*(?:\.\d{0,2})?/
    
    [1-9] - must start with 1 to 9
    \d* - any number of other digits
    (?: )? - non capturing optional group
    \. - a decimal point
    \d{0,2} - 0 to 2 digits
    

    does that work for you? or maybe parseFloat:

    var float = parseFloat( input );
    
    0 讨论(0)
  • 2020-12-14 17:11

    I built my answer from the accepted answer.

    var regex = /^[1-9]\d*(((,\d{3}){1})?(\.\d{0,2})?)$/;

    ^[1-9] The number must start with 1-9
    \d* The number can then have any number of any digits
    (...)$ look at the next group from the end (...)$
    (...)?(...)? Look for two groups optionally. The first is for the comma, the second is for the decimal.
    (,\d{3}){1} Look for one occurance of a comma followed by exactly three digits
    \.\d{0,2} Look for a decimal followed by zero, one, or two digits.

    This regex works off of these rules:

    • Valid values are numbers 0-9, comma and decimal point.
    • If a customer enters more than one decimal point or more than one comma, the value is invalid and will not be accepted.

    • Examples of invalid input values

      • 1.2.3
      • 1,2,4
    • Examples of valid input values
      • 1.23
      • 1,000
      • 3967.
      • 23
      • 1.2
      • 999,999.99

    An example can be seen here: http://jsfiddle.net/rat141312/Jpxu6/1/

    UPDATE

    by changing the [1-9] in the regex to [0-9] any number less than 1 can also be validated. Example: 0.42, 007

    0 讨论(0)
  • 2020-12-14 17:11

     let amount = document.querySelector('#amount'), preAmount = amount.value;
            amount.addEventListener('input', function(){
                if(isNaN(Number(amount.value))){
                    amount.value = preAmount;
                    return;
                }
    
                let numberAfterDecimal = amount.value.split(".")[1];
                if(numberAfterDecimal && numberAfterDecimal.length > 3){
                    amount.value = Number(amount.value).toFixed(3);;
                }
                preAmount = amount.value;
            })
    <input type="text" id="amount">

    0 讨论(0)
提交回复
热议问题