Usando Expressões Regulares com o Controle MaskedTextBox no Visual Basic

This example demonstrates how to convert simple regular expressions to work with the MaskedTextBox control.

Description of the Masking Language

O padrão MaskedTextBox mascaramento idioma se baseia em usada pelo Masked Edit controle na Visual Basic 6.0 e deve ser familiar aos usuários migrando nessa plataforma.

The Mask property of the MaskedTextBox control specifies what input mask to use. The mask must be a string composed of one or more of the masking elements from the following table.

Masking element

Description

Regular expression element

0

Qualquer único dígito entre 0 e 9. Entry required.

\d

9

Digit or space. Entry optional.

[ \d]?

#

Digit or space. Entry optional. If this position is left blank in the mask, it will be rendered as a space. Plus (+) and minus (-) signs are allowed.

[ \d+-]?

L

ASCII letter. Entry required.

[a-zA-Z]

?

ASCII letter. Entry optional.

[a-zA-Z]?

&

Character. Entry required.

[\p{Ll}\p{Lu}\p{Lt}\p{Lm}\p{Lo}]

C

Character. Entry optional.

[\p{Ll}\p{Lu}\p{Lt}\p{Lm}\p{Lo}]?

A

Alphanumeric. Entry optional.

\W

.

Culture-appropriate decimal placeholder.

Not available.

,

Culture-appropriate thousands placeholder.

Not available.

:

Culture-appropriate time separator.

Not available.

/

Culture-appropriate date separator.

Not available.

$

Culture-appropriate currency symbol.

Not available.

<

Converts all characters that follow to lowercase.

Not available.

>

Converts all characters that follow to uppercase.

Not available.

|

Undoes a previous shift up or shift down.

Not available.

\

Escapes a mask character, turning it into a literal. " \\" é a seqüência de escape para uma barra invertida.

\

All other characters.

Literals. All non-mask elements will appear as themselves within MaskedTextBox.

All other characters.

The decimal (.), thousandths (,), time (:), date (/), and currency ($) symbols default to displaying those symbols as defined by the application's culture. You can force them to display symbols for another culture by using the FormatProvider property.

Regular Expressions and Masks

Although you can use regular expressions and masks to validate user input, they are not completely equivalent. Regular expressions can express more complex patterns than masks, but masks can express the same information more succinctly and in a culturally relevant format.

The following table compares four regular expressions and the equivalent mask for each.

Regular Expression

Mask

Notes

\d{2}/\d{2}/\d{4}

00/00/0000

The / character in the mask is a logical date separator, and it will appear to the user as the date separator appropriate to the application's current culture.

\d{2}-[A-Z][a-z]{2}-\d{4}

00->L<LL-0000

A date (day, month abbreviation, and year) in United States format in which the three-letter month abbreviation is displayed with an initial uppercase letter followed by two lowercase letters.

(\(\d{3}\)-)? \d{3}-d{4}

(999)-000-0000

United States phone number, area code optional. If the user does not wish to enter the optional characters, she can either enter spaces or place the mouse pointer directly at the position in the mask represented by the first 0.

$\d{6}.00

$999,999.00

Um valor de moeda no intervalo de 0 a 999999. Os caracteres de moeda, milésimos e decimal serão substituídos em tempo de execução-com sua cultura-equivalentes específicos.

Consulte também

Referência

Mask

MaskedTextBox

Outros recursos

Validação de Cadeias de Caracteres no Visual Basic

Controle MaskedTextBox (Windows Forms)