Mask with Regex and replace to allow single digits and a single hyphen

Asked

Viewed 390 times

2

What I need:

A mask that works in the event keypress of a TextBox replacing what is not numerical and excessive hyphens with "".

What is my difficulty:

Check the entry of only one hyphen in the same expression.

I arrived at the solution using substring and only worked on KeyUP, but wanted to arrive through an expression.

What I’ve already tried:

using System.Text.RegularExpressions;

private static Regex digitsOnly = new Regex(@"(:?[^\d\-])");


private void inputSequencial_KeyUp(object sender, KeyEventArgs e)
{
   if (!String.IsNullOrEmpty(inputSequencial.Text)
   {
      inputSequencial.Text = digitsOnly.Replace(inputSequencial.Text, "");

      //MatchCollection matches = Regex.Matches(inputSequencial.Text, "[\\-]");
      //
      //if (matches.Count > 1)
      //{
      //    for (int i = 1; i <= matches.Count - 1; i++)
      //    {
      //         inputSequencial.Text = inputSequencial.Text.Substring(0, matches[i].Index-1) + inputSequencial.Text.Substring(matches[i].Index, inputSequencial.Text.Length);
      //         inputSequencial.Text = inputSequencial.Text.Replace(inputSequencial.Text[matches[i].Index].ToString(), "");
      //    }
      //}
   }
}

Expected result:

inserir a descrição da imagem aqui

If you know a better way to do that, please point me out. Thank you for your attention.

  • could use a masktextbox ?

  • I believe so, I am migrating from another language to C#/Winforms so I do not know much, I will search how it works masktextbox.

  • 2

    this. Just correcting: MaskedTextBox, then you can wear the mask: "0-000"

1 answer

1


I researched here some alternatives, with Regex or Maskedtextbox(This did not help me much because by default it is not accepted in toolStrip where my textbox was).

In the end the best solution I found was treating the input values to each character as follows:

private void inputSequencial_KeyPress(object sender, KeyPressEventArgs e)
{
   //So permite a entrada de digitos(char 48 à 57), hífen (char 45), backspace(char 8) e delete(char 127)
   if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 45 || e.KeyChar == 8 || e.KeyChar == 127)
   {    
      switch (e.KeyChar)
      {
         case (char)45:
         int count = inputSequencial.Text.Split('-').Length - 1;
         //Se for o primeiro caracter digitado no input ou 
         //se já existir um hífen evito a inserção.
         if (inputSequencial.Text.Length == 0 || count > 0)
         {
            e.Handled = true;
         }
         break;
      }
   }
   else
   {
      e.Handled = true; //Desprezo outras entradas
   }
}

private void inputSequencial_KeyUp(object sender, KeyEventArgs e)
{
   //Se o ultimo caracter for um hífen eu removo ele.
   if (inputSequencial.Text.Length > 1)
   {
      string lastChar = inputSequencial.Text.Substring(inputSequencial.Text.Length - 1, 1);
      if (lastChar == "-")
      {
         inputSequencial.Text.Replace("-", "");
      }
   }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.