Click here to Skip to main content
15,886,534 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
Hi Friends

I need a reguler expression that allows:

1) blank or empty
2) decimal values
3) integer values


C#
[Required(ErrorMessage = "rate 1 is required")]
[RegularExpression(@"[0-9]*\.?[0-9]*$", ErrorMessage = "rate 1 is in numeric formate")]
public decimal Rate1 { get; set; }


Please help me, the above expression is not allowing null values.

Thanks
Posted
Updated 29-Aug-12 4:11am
v4

You have the Required attribute on there. If a value is required it cannot be null. The regular expression has nothing to do with it.
 
Share this answer
 
So, you allow (I assume with leading and trailing spaces)
1. lines of only spaces, zero to any number of them
2. plain integral numbers
3. numbers with decimal part and optional part before the decimal part

The regex for these individual parts is:
1. ^\s*$
2. ^\s*\d+\s*$
3. ^\s*\d*\.\d+\s*$

Putting it all together and combine common parts:
    begin, optional-spaces, optional-number, optional-spaces, end
or in Regex:
    ^\s*(?:\d+|\d*\.\d+)?\s*$

Cheers
Andi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900