Skip to content Skip to sidebar Skip to footer

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). numeri

Solution 1:

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

/^[-+]?[0-9]+(\.[0-9]+)?$/

Solution 2:

Use this regular expression:

^\d+(\.\d+)?$

Solution 3:

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

Solution 4:

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

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

Post a Comment for "Numeric Values Only"