Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

javascript - Regex currency validation

I need Help for currency regex in jQuery function.

  • It optionally allows "$" sign only one time in beginning.
  • It allows comma as digital-group-separator, but not in the beginning or the end.
  • It allows only 2 digits rounded after decimal point.
  • It allows only one decimal point and not in the beginning or the end.

Valid:

$1,530,602.24
1,530,602.24

Invalid:

$1,666.24$
,1,666,88,
1.6.66,6
.1555.

I tried /^$?[0-9][0-9,]*[0-9].?[0-9]{0,2}$/i; it works fine except it matches 1,6,999.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The RegEx

// 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"

The RegEx Breakdown

  • When the optional parts are too liberal, we need to look ahead and guarantee there's a number: (?=.*d)
  • May or may not start with a dollar sign (I assume negatives are invalid): ^$?
    • Follow that with -? to allow negative numbers
  • Begins with 1-3 numbers: [1-9]d{0,2}
    • Could almost be (d{1,3}), but that would allow "0,123"
    • One exception, can start with 0 in the case of "$0.50" or "0.50": |0
    • These regexes assume multiple leading 0's are invalid
  • Any number of three digit numbers separated by comma: (,d{3})*
    • Remove ? before . if you want to disallow numbers starting with "$."
  • Requires or allows decimal (one or two digits): .d{1,2} or (.d{1,2})? respectively
  • End with $ (unescaped) to make sure there's nothing after a valid number (like $1,000.00b)

To use the regex, use the string's match method and encase the regex between two forward slashes.

// 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})?$/);

Basic Usage Example

Demo with Test Cases

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/>');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...