Mask for variable size input field with windows Forms

Asked

Viewed 38 times

0

How to make an email mask in a Windows Forms application ? To using Maskedtextbox that to validate fixed size fields, example: ZIP CODE and CPF the problem is when the mask is of varied size for example the email ?

  • You’ve already taken a look at the Event Control. ??

  • I don’t know, but I’ll look it up and get back to you.

1 answer

0

You can use namespace System.Text.RegularExpressions; to generate a rule and calculate it where you wish.

I created a very simple example to illustrate the use of the class Regex, where I run the rule inside an event click, to show the rule usage. Follow Image below:

inserir a descrição da imagem aqui

As you can see very simple, more knowing the namespace better you will see that the possibilities are numerous.

  • CPF: "^(\d{3}.\d{3}.\d{3}-\d{2})|(\d{11})$"
  • ZIP CODE: ^\d{5}-\d{3}$
  • EMAIL: "^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$"

Rule used in the example:

Regex regra = new Regex(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");

Browser other questions tagged

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