use*_*924 29 javascript regex currency
我需要在jQuery函数中使用货币正则表达式的帮助.
有效:
$1,530,602.24
1,530,602.24
Run Code Online (Sandbox Code Playgroud)
无效:
$1,666.24$
,1,666,88,
1.6.66,6
.1555.
Run Code Online (Sandbox Code Playgroud)
我试过了/^\$?[0-9][0-9,]*[0-9]\.?[0-9]{0,2}$/i; 除了匹配之外它工作正常1,6,999.
Gar*_*ary 111
// Requires a decimal and commas
^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?\.\d{1,2}$
// Allows a decimal, requires commas
(?=.*\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?(\.\d{1,2})?$
// Decimal and commas optional
(?=.*?\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|\d+)?(\.\d{1,2})?$
// Decimals required, commas optional
^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?\.\d{1,2}$
// *Requires/allows X here also implies "used correctly"
Run Code Online (Sandbox Code Playgroud)
(?=.*\d)^\$?
-?允许负数[1-9]\d{0,2}
(\d{1,3}),但这将允许"0,123"|0(,\d{3})*
?在之前删除\..\.\d{1,2}或(\.\d{1,2})?分别$(未转义)结束以确保在有效数字之后没有任何内容(例如$ 1,000.00b)要使用正则表达式,请使用字符串的match方法并将正则表达式包含在两个正斜杠之间.
// The return will either be your match or null if not found
yourNumber.match(/(?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|0)?(\.[0-9]{1,2})?$/);
// For just a true/false response
!!yourNumber.match(/(?=.)^\$?(([1-9][0-9]{0,2}(,[0-9]{3})*)|0)?(\.[0-9]{1,2})?$/);
Run Code Online (Sandbox Code Playgroud)
var tests = [
"$1,530,602.24", "1,530,602.24", "$1,666.24$", ",1,666,88,", "1.6.66,6", ".1555."
];
var regex = /(?=.*\d)^\$?(([1-9]\d{0,2}(,\d{3})*)|0)?(\.\d{1,2})?$/;
for (i = 0; i < tests.length; i++) {
console.log(tests[i] + ' // ' + regex.test(tests[i]));
document.write(tests[i] + ' // ' + regex.test(tests[i]) + '<br/>');
}Run Code Online (Sandbox Code Playgroud)
Bre*_*ent -1
这是应该为您实现此目的的正则表达式。
开头必须是数字或 $ 符号。带逗号的数字可以是任意数量,但必须以数字开头和结尾。行尾可以选择有一个小数点,其后最多有两位数。
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!");
Run Code Online (Sandbox Code Playgroud)
或者使用这个功能
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);
}
Run Code Online (Sandbox Code Playgroud)
查看它的工作原理: http: //jsfiddle.net/znuJf/