Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

Can any one help me in generating the regex for only acceptable floating values where one can only able to add one decimal inside value

For Ex:-

12.5 -------------Allowed to type
12.5. -------------Don't allowed to type second decimal
12 -------------Allowed without any decimal
.12 -------------Allowed decimal in first place
12. -------------It's also ok to allow


I have one regex which accepts floating values i.e :-
Regex regex = new Regex("[^0-9.]+");

But the problem in it is that you can enter more than one decimal.
Any Solution.....

Thanks In Advance
Posted
Comments
Zoltán Zörgő 17-Mar-15 16:00pm    
Why is this allowed, it has two decimals?
.12 -------------Allowed decimal in first place
binadi007 17-Mar-15 16:11pm    
only one decimal i.e ".", where is it two time
Here decimal means period
Sergey Alexandrovich Kryukov 17-Mar-15 16:36pm    
What is the exact TextBox type? Full name, please.
—SA

1 solution

I think this expression will do it:
\d*\.?\d?
 
Share this answer
 
Comments
binadi007 17-Mar-15 16:12pm    
"\" shows red line below it how to write "\" symbol?
Richard Deeming 17-Mar-15 16:16pm    
For C#, you'll either need to escape the \ character:
"\\d*\\.?\\d?"
or use a verbatim string:
@"\d*\.?\d?"
binadi007 17-Mar-15 16:23pm    
still shows error......incorrect regex for floating values with only one "."
Zoltán Zörgő 17-Mar-15 16:29pm    
It is correct, but try this:
@"^\d*\.?\d?$"
You were not using it correctly...
binadi007 17-Mar-15 16:38pm    
It's not right.....I tried it and still able to write alphabets in the textbox,
Where with this code :- "[^0-9.]+"
I'm not able to write alphabets

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