当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Numeric Values only

问题描述:

Possible Duplicate:

Numeric validation with RegExp to prevent invalid user input

I am new to regex.

Please help writing pattern for numeric values only (for JavaScript).

  1. numeric values only.
  2. allowed decimal "."
  3. no commas.

Thanks!

网友答案:

Here is a great resource for playing around with various regular expressions in JavaScript. Your particular expression looks like this:

/^[-+]?[0-9]+(\.[0-9]+)?$/
网友答案:

Use this regular expression:

^\d+(\.\d+)?$
网友答案:

Rather than using a RegEx and taking care of all permutations of a float number, I would suggest following code to check if it is a valid float number:

var s = ".45";
var d = parseFloat(s);
if (!isNaN(d))
   alert("valid float: " + d);

If you have to have a regex then I would suggest:

/^[-+]?(?=.)\d*(?:\.\d+)?$/
网友答案:

Check out the example here for floating point number regex samples...

http://www.regular-expressions.info/floatingpoint.html

分享给朋友:
您可能感兴趣的文章:
随机阅读: