使用JavaScript进行印度货币验证的正则表达式

时间:2011-05-13 05:25:03

标签: javascript regex

有人可以告诉我如何使用正则表达式检查文本框中输入的金额(印度货币)是否有效?

我的条件很少..

  1. 该数量不应超过1个小数点,但可以包含一个小数点。
  2. 如果有小数点,则后面应跟一个或多个数字。
  3. 金额应该只有数字,最多只能有一个小数点。
  4. 如果我输入的金额为10.000,则不应接受,因为它在小数点后有3个连续的零。但是应该接受56.8906。
  5. 如果金额从零开始(即0123),则不应接受,但应接受0.0

5 个答案:

答案 0 :(得分:4)

^(?:0|[1-9]\d*)(?:\.(?!.*000)\d+)?$

应该做你想做的事。

<强>解释

^         # Start of string.
(?:       # Try to match...
 0        # either a 0
|         # or
[1-9]\d*  # an integer number > 0, no leading 0 allowed.
)         # End of integer part.
(?:       # Try to match...
 \.       # a decimal point.
 (?!      # Assert that it's not possible to match
  .*000   # any string that contains 000 from this point onwards.
 )        # End of lookahead assertion.
 \d+      # Match one or more digits.
)?        # End of the (optional) decimal part
$         # End of string.

在JavaScript中:

curRegExp = /^(?:0|[1-9]\d*)(?:\.(?!.*000)\d+)?$/;

答案 1 :(得分:1)

格式编号1000000到12,34,567.00

<input id="item-purchase-amt" class="col-sm-12 form-control-plaintext tr-input" type="text" data-type="currency">

$(document).on('keyup', "input[data-type='currency']", function () {
formatCurrency($(this));
});
$(document).on('blur', "input[data-type='currency']", function () {
formatCurrency($(this), "blur");
});

function currencyNumber(n) {
// format number 1000000 to 1,234,567
// return n.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")
// format number 1000000 to 12,34,567
return n.replace(/\D/g, "").replace(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g, "$1,");
}

function formatCurrency(input, blur) {
var input_val = input.val();
if (input_val === "") { return; }
var original_len = input_val.length;
var caret_pos = input.prop("selectionStart");
if (input_val.indexOf(".") >= 0) {
    var decimal_pos = input_val.indexOf(".");
    var left_side = input_val.substring(0, decimal_pos);
    var right_side = input_val.substring(decimal_pos);
    left_side = currencyNumber(left_side);
    right_side = currencyNumber(right_side);
    if (blur === "blur") {
        right_side += "00";
    }
    right_side = right_side.substring(0, 2);
    input_val = left_side + "." + right_side;

} else {
    input_val = currencyNumber(input_val);
    input_val = input_val;
    if (blur === "blur") {
        input_val += ".00";
    }
}
input.val(input_val);
var updated_len = input_val.length;
caret_pos = updated_len - original_len + caret_pos;
input[0].setSelectionRange(caret_pos, caret_pos);
}

答案 2 :(得分:1)

$(this).replace(/\B(?=(?:\d{3})+(?!\d))/g, ','); 
/*For US number system (millions & billions)*/

$(this).replace(/\B(?=(?:(\d\d)+(\d)(?!\d))+(?!\d))/g, ',');
/*For Indian number system (lakhs & crores)*/

这些将格式化相应系统中的值。

答案 3 :(得分:0)

印度货币正则表达式

/^(\d{1,2})(,\d{2})*(,\d{1,3}){1}(\.\d{1,})?$/g

美国货币正则表达式

/^(\d{1,3})(,\d{3})*(\.\d{1,})?$/g

答案 4 :(得分:0)

印度货币编号转换正则表达式

$(this).val(parseFloat($(this).val(), 10).toFixed(2).replace(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g, "$1,").toString());

外币号码转换正则表达式

$(this).val(parseFloat($(this).val(), 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());