By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

Need help on regular expression in .Net:

P: n/a
I would like to get some help on regular expression to validate
decimal numbers. It should also match negative value, empty value.

The expression:
^(\-){0,1}([1-9]{1}[0-9]{0,8}(\,[0-9]{3})*(\.[0-9]{0,8})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,8})?|0(\.[0-9]{0,8})?|(\.[0-9]{1,8})?)$
is doing this job, but matches a single hyphen "-" also.

For example: matches -12.34 but also matches -

I need help to have this expression modified to unmatch single hyphen
sign ("-") as only letter in the input.

Thanks in Advance.
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Daniel wrote:
I would like to get some help on regular expression to validate
decimal numbers. It should also match negative value, empty value.


I can't help you on the regex front (only just about to read "Mastering
Regular Expressions", I'm afraid), but for this particular case why don't
you use a compare validator and check for the Double datatype? e.g.

<asp:TextBox id="txtDoubleField" runat="server"/>
<asp:CompareValidator
ControlToValidate="txtDoubleField"
Operator="DataTypeCheck"
Type="Double"
ErrorMessage="- you must provide a valid Double Value"
Display="None"
runat="server"/>

Combine this with a RequiredFieldValidator (or not if you want to allow an
empty value) and a ValidationSummary and you have a much easier way to check
user input and give feedback (in this case at least).

This is, of course, assuming you *are* using ASP.NET and checking user
input. If not, I'm sure someone else will have an answer...

--
jo inferis
Nov 18 '05 #2

P: n/a
The problem is you have made the entire rest of the expression optional with
the () around it. So - will always be true. The quickest solution is to move
the (.

^((\-){0,1}[1-9]{1}[0-9]{0,8}(\,[0-9]{3})*(\.[0-9]{0,8})?|[1-9]{1}[0-9]{0,}(
\.[0-9]{0,8})?|0(\.[0-9]{0,8})?|(\.[0-9]{1,8})?)$

Do not have the time to optimize, but you will find that this no longer
finds '-' valid.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
"Daniel" <pr**********@vizual.co.in> wrote in message
news:35**************************@posting.google.c om...
I would like to get some help on regular expression to validate
decimal numbers. It should also match negative value, empty value.

The expression:
^(\-){0,1}([1-9]{1}[0-9]{0,8}(\,[0-9]{3})*(\.[0-9]{0,8})?|[1-9]{1}[0-9]{0,}(
\.[0-9]{0,8})?|0(\.[0-9]{0,8})?|(\.[0-9]{1,8})?)$ is doing this job, but matches a single hyphen "-" also.

For example: matches -12.34 but also matches -

I need help to have this expression modified to unmatch single hyphen
sign ("-") as only letter in the input.

Thanks in Advance.

Nov 18 '05 #3

P: n/a
Without the ( for rest of the expression works. I am going to use it for now!!

Thanks Tampa, Jo Inferis and Cowboy.
Nov 18 '05 #4

P: n/a
Without the ( for rest of the expression works. I am going to use it for now!!

Thanks Tampa, Jo Inferis and Cowboy.
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.