Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF
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
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 28-Jan-13 22:52pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
^\d+\.\d{2}$
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.
  Permalink  
Comments
Edo Tzumer at 29-Jan-13 5:21am
   
5+
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Got a Solution
 
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));
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 29 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid