Can anyone please tell me how to check whether the entered amount (Indian currency) in a textbox is valid or not using regular expression?
I have few conditions..
- The amount should not contain more than 1 decimal point, but can have a single decimal point.
- If there is a decimal point then it should be followed by one or more digits. 开发者_StackOverflow
- The amount should have only numbers and at most one decimal point.
- If I enter an amount like 10.000 then it should not be accepted because it has 3 continuous zeros after decimal point. but 56.8906 should be accepted.
- If the amount starts with zero (ie 0123) it should not be accepted, but 0.0 should be accepted
$(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)*/
These will format the value in the respective systems.
^(?:0|[1-9]\d*)(?:\.(?!.*000)\d+)?$
should do what you want.
Explanation:
^ # 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.
In JavaScript:
curRegExp = /^(?:0|[1-9]\d*)(?:\.(?!.*000)\d+)?$/;
format number 1000000 to 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);
}
Indian Currency RegEx
/^(\d{1,2})(,\d{2})*(,\d{1,3}){1}(\.\d{1,})?$/g
US Currency RegEx
/^(\d{1,3})(,\d{3})*(\.\d{1,})?$/g
Indian Currency Number Conversion RegExp
$(this).val(parseFloat($(this).val(), 10).toFixed(2).replace(/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/g, "$1,").toString());
Foreign Currency Number Conversion RegExp
$(this).val(parseFloat($(this).val(), 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString());
精彩评论