Click here to Skip to main content
12,621,228 members (34,892 online)
Rate this:
 
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 23: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 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100