locked
c# regular expression RRS feed

  • Question

  • Hi all I want to set a regular expression to allow only decimal input in the textBox ... I have this, but it doesn't work ...I dont know why ...

    xaml
    <TextBox Name="txt1" PreviewTextInput="txt1_PreviewTextInput"/>
    
    c#
    private void txt1_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        Regex regex = new Regex(@"^\d+[\.,]?\d*$");
        if (!regex.IsMatch(e.Text))
        {
            e.Handled = true;
        }
    } 

    can you help me ?

     
    • Edited by TakeshiKitano Wednesday, June 4, 2014 9:24 AM
    • Moved by Mike Danes Wednesday, June 4, 2014 9:34 AM WPF specific actually
    Wednesday, June 4, 2014 9:24 AM

Answers

  • Hi, thank you ... but I forgot that this finction is for more textboxes ... all have same function ... so you probably work with one textBox ...


    ... text.Remove(txt1.SelectionStart + 1, txt1...


    You could just cast the sender parameter to a TextBox and operate on this one:

      private void txt1_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
       TextBox txtBox = sender as TextBox;
       Regex regex = new Regex(@"^\d+[\.,]?\d*$");
       string text = txtBox.Text.Insert(txtBox.SelectionStart, e.Text);
       if (txtBox.SelectionLength > 0)
       {
        text = text.Remove(txtBox.SelectionStart + 1, txtBox.SelectionLength);
       }
       if (!regex.IsMatch(text))
       {
        e.Handled = true;
       }
      } 
    

    Wednesday, June 4, 2014 9:57 AM

All replies

  • Hi, please try this:

    private void txt1_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        Regex regex = new Regex(@"^\d+[\.,]?\d*$");
        string text = txt1.Text.Insert(txt1.SelectionStart, e.Text);
        if (txt1.SelectionLength > 0)
        {
            text = text.Remove(txt1.SelectionStart + 1, txt1.SelectionLength);
        }
        if (!regex.IsMatch(text))
        {
            e.Handled = true;
        }
    } 

    Wednesday, June 4, 2014 9:42 AM
  • Hi, thank you ... but I forgot that this finction is for more textboxes ... all have same function ... so you probably work with one textBox ...


    ... text.Remove(txt1.SelectionStart + 1, txt1...

    Wednesday, June 4, 2014 9:51 AM
  • Hi, thank you ... but I forgot that this finction is for more textboxes ... all have same function ... so you probably work with one textBox ...


    ... text.Remove(txt1.SelectionStart + 1, txt1...


    You could just cast the sender parameter to a TextBox and operate on this one:

      private void txt1_PreviewTextInput(object sender, TextCompositionEventArgs e)
      {
       TextBox txtBox = sender as TextBox;
       Regex regex = new Regex(@"^\d+[\.,]?\d*$");
       string text = txtBox.Text.Insert(txtBox.SelectionStart, e.Text);
       if (txtBox.SelectionLength > 0)
       {
        text = text.Remove(txtBox.SelectionStart + 1, txtBox.SelectionLength);
       }
       if (!regex.IsMatch(text))
       {
        e.Handled = true;
       }
      } 
    

    Wednesday, June 4, 2014 9:57 AM
  • Thank you so much :-)
    Wednesday, June 4, 2014 10:03 AM