Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
hi,
I have an textbox which will accept only numeric and decimal values. i have used the below regular expression to statisfy this condition.

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


The max length of the text box is 4.

And the textbox will accept max of two digits before decimal point and single digit after the decimal point.. To achieve this am using the below regex expression:

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


This will give the output as 1.4,12.5 etc..

So now when the user enters more than one decimal point like 12.. or 2... how should i restrict?

can anyone please help me?

[edit]Code block added[/edit]
Posted
Updated 2-Mar-14 23:20pm
v3
Comments
BillWoodruff 3-Mar-14 4:37am    
If your question involves WPF, then take the time to tag your question !

I agree with d@nish's answer: I think you should use a MaskedTextBox.

Simple way is to check number of Decimal places on each keypress. Following is stub for same,

VB
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 46 Then
        If InStr(Text1.Text, ".") > 1 Then
            KeyAscii = 0
        End If
    End If
End Sub


This will restrict user from entering two or more decimal places. Now add your logic to prevent user from entering Decimal Places at the start and end of the input string.

Hope this helps you.
 
Share this answer
 
Comments
Member 10593922 3-Mar-14 4:34am    
am not able to use key press event in wpf. As well as am not able to use keyAscii
If you are doing this in a Windows Form based application, make use of MaskedTextBox. If this is web based application, you can make use of MaskedEdit control that comes with Ajax toolkit.

Edit: Since you are using Windows form, this is a better way to do it:

C#
NumericUpDown numericUpDown = new NumericUpDown();
           numericUpDown.Maximum = 99.9M;
           numericUpDown.Minimum = 0.0M;
           numericUpDown.DecimalPlaces = 1;


Edit 1: There no NumericUpDown in WPF. You can get one from Extended WPF toolkit or build your own.
 
Share this answer
 
v4
Comments
BillWoodruff 3-Mar-14 4:45am    
+5
Member 10593922 3-Mar-14 4:47am    
thank for the reply.. can u please show the sample format how to use MaskedTextBox?
dan!sh 3-Mar-14 4:54am    
Updated the reply.
Member 10593922 3-Mar-14 5:03am    
am not able to use numericUpDown event in wpf..
dan!sh 3-Mar-14 5:14am    
See this is why you should use appropriate tags. You are getting responses for web application, windows forms and what not since people have no clue what application you are making.
refer this my hekp you..
XML
<html>
  <head>
    <script>
      function checkDec(xElement) {
    theNum = xElement.value.toString();
    var regExp = /^\d{0,}\.\d{2}$/;    //format  .## required
        //var regExp = /^\d{1,}\.\d{2}$/;  //format #.## required
        var formatLine = theNum.match(regExp);
        if(!formatLine){ //Test if there was no match
  alert("ERROR:\n\nThe amount entered: " + theNum + " is not in the correct format of .##"); //Display Error
  xElement.focus();  //Force User To Enter Correct Amount
}
  }
</script>
  </head>
  <body>
    <form name="test">
      Input Money Amount $<input type="text" name="money" onchange="checkDec(this)">
</form>
  </body>
</html>
 
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