smacca
asked on
Positive decimal value regular expression with max 3 decimal places
I need to update my regular expression so it has the following:
* Accepts all positive decimal values (currently only accepts values greater and equal to one (1))
* i.e accepts ---> 0.0001
* Can have maximum 3 decimal places.
* Decimal places are optional.
This regular expression will be used to validate currency input.
* Accepts all positive decimal values (currently only accepts values greater and equal to one (1))
* i.e accepts ---> 0.0001
* Can have maximum 3 decimal places.
* Decimal places are optional.
This regular expression will be used to validate currency input.
ValidationExpression="^\d*[1-9]\d*(\.\d+)?$"
^[1-9]\d*(\.\d{1,3})?$
^(?=\d*(\.\d{1,3})?$)(?=.* [1-9])
Ah .. my comment above I was misunderstood to accept only values greater than 0 ;)
Please ignore it.
Please ignore it.
ASKER
None of the above solutions work. Have you guys tested??
I'm just confused. You said that "Can have maximum 3 decimal places", but why accepts 0.0001. It's 4 decimal places. To accept it (4 decimal), just use ozo's regex but change from \d{1,3} to \d{1,4}
Here output of code below (I modified the regex a bit from ozo)
[123] = 123 (True) parse = success
[ 123 ] = 123 (True) parse = success
[01.1234] = 1.1234 (True) parse = success
[1.1234] = 1.1234 (True) parse = success
[-1.123] = -1.123 (False) parse = success
[-0.0] = 0.0 (False) parse = success
[0.0] = 0.0 (True) parse = success
[00.0] = 0.0 (True) parse = success
[.0001] = 0.0001 (True) parse = success
[0.0001] = 0.0001 (True) parse = success
[a0.0001] = # (False) parse = failed
[ ] = # (False) parse = failed
Here output of code below (I modified the regex a bit from ozo)
[123] = 123 (True) parse = success
[ 123 ] = 123 (True) parse = success
[01.1234] = 1.1234 (True) parse = success
[1.1234] = 1.1234 (True) parse = success
[-1.123] = -1.123 (False) parse = success
[-0.0] = 0.0 (False) parse = success
[0.0] = 0.0 (True) parse = success
[00.0] = 0.0 (True) parse = success
[.0001] = 0.0001 (True) parse = success
[0.0001] = 0.0001 (True) parse = success
[a0.0001] = # (False) parse = failed
[ ] = # (False) parse = failed
string[] test = {
"123",
" 123.0 ",
"01.1234",
"1.1234",
"-1.123",
"-0.0",
"0.0",
"00.0",
".0001",
"0.0001",
"a0.0001",
" a"
};
Regex rx = new Regex(@"^\s*(?=\d*(\.\d{1,4})?\s*$)(?=.*[0-9])");
foreach (string s in test)
{
Console.Write("["+s+"] \t= ");
string ps = "success", d = "#";
try { d = Decimal.Parse(s).ToString(); } catch (Exception) { ps = "failed"; }
Console.WriteLine(d+" ("+rx.IsMatch(s)+")\t\tparse = "+ps);
}
ASKER
Still not working for some reason - I understand your code works fine but it doesnt translate to my example. Can you have a look at the code below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtTest" Width="250" runat="server" />
<asp:RegularExpressionValidator Enabled="true" ID="revPositiveCurrency" ControlToValidate="txtTest" Display="None" ErrorMessage="Positive decimal currency only!" ValidationExpression="^\s*(?=\d*(\.\d{1,4})?\s*$)(?=.*[0-9])" runat="server" /
<asp:Button ID="btnSubmit" Text="SUBMIT" runat="server" />
<asp:ValidationSummary ID="vsSummary" DisplayMode="BulletList" ShowSummary="true" HeaderText="Errors:" runat="server" />
</div>
</form>
</body>
</html>
do you need \\ in quotes
ASKER
You will likely need \\ in the quotes. Your code uses "@" to make the statement a literal syntax meaning you do not have to \\ things etc. I will try out now and get back to you.
ASKER
Cannot sort out myself - I do not know what to \\ in the statement. If you have IIS, you can test the page I provided very easily. Thanks for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No exact answer provided.