Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
See more:
In my windows application. I have amount textbox field.It will accept total 11 with 2 decimal places.If i didn't enter decimal places it should allow only 8 places.else It will allow 11 places.how to write validation for this.I'm fresher don't get any idea to try.
Please help me on this.
Thanks in advance.

What I have tried:

I'm fresher don't get any idea to try.
Please help me on this.
Thanks in advance.
Posted
Updated 26-Apr-17 18:35pm
v2

You could use a regex validation on that, a short googling or reading of this[^] should get you into it. With a regex matching you could easily implement your check and make sure only numbers are put in.

On the other hand you could check by your self by validating the inserted string with substring or contains.

For example check if the string is longer than 8 chars and then check if the string contains a '.' at the 8th (9th) place.
 
Share this answer
 
Comments
Maciej Los 26-Apr-17 16:14pm    
5ed!
private void txtamount_KeyPress(object sender, KeyPressEventArgs e)
       {
            string[] arr = txtamount.Text.Split('.');

           if (arr.Length == 1)
           {
               if (txtamount.Text.Length > 7 && e.KeyChar != '.' && e.KeyChar != (char)Keys.Back)
               {
                   e.Handled = true;
               }

           }
       }
 
Share this answer
 
Comments
HobbyProggy 27-Apr-17 2:00am    
I see you understood :)
Member 13153537 27-Apr-17 3:26am    
yes thank you

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