required and regular expression for float datatype
I want to validate the field whose datatype is float in th开发者_如何学Goe database.Please tell me the required expression and regular expression for the float datatype so that user can enter only the digits in float.
for example this is giving error
else if(!Regex.IsMatch(lowerlimit,
@"[+-]?(?:(?:\d+\.\d*)|(?:\d*\.\d+))"))
{
ModelState.AddModelError("lowerlimit", "Please enter valid lower limit");
}
Thanks Ritz
[+\-]?(?:(?:\d+(\.\d*)?)|(?:\d*\.\d+))
You can drop the non-capturing ?:
tags if you want to make it more readable.
[+\-]?((\d+(\.\d+)?)|(\d*\.\d+))
Note: this regex will match only floating point numbers with a decimal point - not integers.
Try something like this:
^\d*\.?\d*$
1st: It looks like you've fallen in the common trap: You had one problem, you decided to solve it with regular expressions, and now you have two problems.
You should see if you can use your standard library to try and parse the prospective float into a float, to avoid having to mess around with approximate regular expressions.
Depends what styles of floating point number text representation you want to allow.
Scientific notation:
[-+]?\d(\.\d+)?([eE]-?\d+)?
Simple decimal point:
-?\d+(\.\d+)?
Allow empty integer part:
[-+]?((\d*\.\d+)|(\d+(\.\d+)?))
精彩评论