试试这个:
if (/^\d+([,.]\d{1,2})?$/im.test(value)) {
alert("ACCEPTED\n" + value);
} else {
alert("REJECTED\n" + value);
}
}
JSFIDDLE EXAMPLE
正则表达式解释
^\d+([,.]\d{1,2})?$
Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed, line feed, line separator, paragraph separator) «^»
Match a single character that is a “digit” (ASCII 0–9 only) «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regex below and capture its match into backreference number 1 «([,.]\d{1,2})?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character from the list “,.” «[,.]»
Match a single character that is a “digit” (ASCII 0–9 only) «\d{1,2}»
Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed, line feed, line separator, paragraph separator) «$»