Skip to content

Add link to RegEx docs #11425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@
<remarks>
<format type="text/markdown"><![CDATA[

## Remarks
You apply the RegularExpressionAttribute attribute to a property when you need to validate values for the property against a regular expression. The regular expression enables you to specify very precisely the format of valid values. The Pattern property contains the regular expression. If the value of the property is null or an empty string (""), the value automatically passes validation for the RegularExpressionAttribute attribute. To validate that the value is not `null` or an empty string, use the **RequiredAttribute** attribute.
## Remarks

The regular expression searches for an exact match, not using `^` before and `$` at the end of the pattern produces the same results as using it. For a search hit, prepend and append the pattern with `.*`.



## Examples
The following example shows how to use the <xref:System.ComponentModel.DataAnnotations.RegularExpressionAttribute> attribute to validate the FirstName and LastName data fields. The regular expression allows up to 40 uppercase and lowercase characters. The example performs the following tasks:

- Implements a metadata partial class and the associated metadata class.

- In the associated metadata class, applies the <xref:System.ComponentModel.DataAnnotations.RegularExpressionAttribute> attribute to the FirstName and LastName data fields, specifying the pattern and custom error messages.
You apply the RegularExpressionAttribute attribute to a property when you need to validate values for the property against a regular expression. The regular expression enables you to specify very precisely the format of valid values. The Pattern property contains the regular expression. If the value of the property is null or an empty string (""), the value automatically passes validation for the RegularExpressionAttribute attribute. To validate that the value is not `null` or an empty string, use the **RequiredAttribute** attribute.

The regular expression searches for an exact match, not using `^` before and `$` at the end of the pattern produces the same results as using it. For a search hit, prepend and append the pattern with `.*`.

## Examples

The following example shows how to use the <xref:System.ComponentModel.DataAnnotations.RegularExpressionAttribute> attribute to validate the FirstName and LastName data fields. The regular expression allows up to 40 uppercase and lowercase characters. The example performs the following tasks:

:::code language="csharp" source="~/snippets/csharp/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RegularExpressionAttribute/CS/Customer.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RegularExpressionAttribute/VB/Customer.vb" id="Snippet1":::
- Implements a metadata partial class and the associated metadata class.
- In the associated metadata class, applies the <xref:System.ComponentModel.DataAnnotations.RegularExpressionAttribute> attribute to the FirstName and LastName data fields, specifying the pattern and custom error messages.

:::code language="csharp" source="~/snippets/csharp/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RegularExpressionAttribute/CS/Customer.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_WebNet/System.ComponentModel.DataAnnotations.RegularExpressionAttribute/VB/Customer.vb" id="Snippet1":::

]]></format>
</remarks>
<related type="Article" href="https://learn.microsoft.com/dotnet/standard/base-types/regular-expressions">.NET regular expressions</related>
</Docs>
<Members>
<Member MemberName=".ctor">
Expand Down