Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Que 1)i would like to have only decimal point after value entered and two decimal points later.
No other special character other than .(dot) or alphabet is allowed

my code is
C#
private static bool IsTextNumeric(string str)
{
  System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("[^0-9^.{1}^0-9]$");
            return reg.IsMatch(str);
}
 private void NumericOnly(System.Object sender, System.Windows.Input.TextCompositionEventArgs e)
{
   e.Handled = IsTextNumeric(e.Text);
}

private void txtbx_Capcity_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
   NumericOnly(txtbx_Capcity, e);
}



Que 2)Also I would like restrict the value in text box between Min and Max if possible using regex.

Thanks in Advance for Ur help
Posted

Your regex won't work. "." is a "match any character" character in a rexeg, so for a literal decimal point, you want "\."
If you are after numbers, point, two numbers then try:
C#
^\d+\.\d{2}$

C#
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^\d+\.\d{2}$");


Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
   
Comments
Joezer BH 29-Jan-13 5:21am
   
5+
Got a Solution

C#
private void txtbx_Capcity_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
  System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");
  e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart, e.Text));
}
   

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