Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Am having a texbox which accepts only numeric and decimal values. As well to accepts max of two digits before the decimal and one digit after the decimal point using the following regex.

Regex reg = new Regex("[^[0-9.]+");

Regex re = new Regex("^\\d{1,2}([.]\\d{1})$");

And i have added an condition to accept only one decimal point using :

if (e.Text == "." && txtPlannedHours.Text.IndexOf('.') > -1)
{
e.Handled = true;
}
in the previewTextInput event.


The valid values which the user can enter are:
1111,1.5,12.5 etc..


Invalid value which user should not be allowed to enter is :
111. or 234. etc..


So how can i avoid user from entering a decimal point in the last, i.e after entering 3 digits?(at the 4th position of the textbox length)

The max length of textbox is given as 4 in wpf(xaml)
Posted

WRite the following code in Javascript function

C#
if (s.indexOf('.') >= 0) {
    return false;
}



and cal the function in ONkey press event for textbox
 
Share this answer
 
Comments
Member 10593922 4-Mar-14 5:34am    
Thank you for the reply but i want the solution in C#..
nandakishoreroyal 4-Mar-14 5:37am    
You can handle it in server side and show error message.

we can not restrict not to enter decimal at fourth place with out using javascript.
I think you can use the regular expression (((\d{1,3})(,\d{3})*)|(\d+))(.\d{2})?
Its works for me. It will handle your comma in required place and also the dot.

Thanks
 
Share this answer
 
Comments
Member 10593922 4-Mar-14 5:39am    
does the above regular expression accepts max of only two digits before the decimal point,followed by single decimal point,followed by single digit.
As well it should allow only one decimal point..
Member 10593922 4-Mar-14 5:41am    
the problem which am facing is the text box is allowing me to enter decimal point at the end also like ex: 111. , which is wrong..
so when user tries to enter decimal point at the end, it should not allow
Member 10593922 4-Mar-14 5:42am    
there is no option to accept comma, i have just given some examples separated by comma.
The text box will accept only numeric or decimal values only

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