Элемент управления RegularExpressionValidator выполняет проверку значения по шаблону регулярного выражения.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
-
<
meta
charset
=
"utf-8"
/>
-
<
title
></
title
>
-
<
style
>
-
td {
-
padding: 5px;
-
}
-
.b {
-
text-align: right;
-
}
-
.c {
-
text-align: center;
-
}
-
</
style
>
</
head
>
<
body
>
-
<
form
id
=
"form1"
runat
=
"server"
>
-
<
div
>
-
<
table
>
-
<
tr
>
-
<
td
>
-
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text
=
"E-MAIL"
></
asp:Label
>
-
</
td
>
-
<
td
>
-
<
asp:TextBox
ID
=
"TextBox1"
runat
=
"server"
></
asp:TextBox
>
-
</
td
>
-
<
td
>
-
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ErrorMessage
=
"RequiredFieldValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
>Заполните поле</
asp:RequiredFieldValidator
>
-
</
td
>
-
<
td
>
-
<
asp:RegularExpressionValidator
ID
=
"RegularExpressionValidator1"
runat
=
"server"
ErrorMessage
=
"RegularExpressionValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
ValidationExpression
=
"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
>Неверно</
asp:RegularExpressionValidator
>
-
</
td
>
-
</
tr
>
-
<
tr
>
-
<
td
>
-
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"OK"
OnClick
=
"Button1_Click"
/>
-
</
td
>
-
<
td
></
td
>
-
<
td
></
td
>
-
<
td
></
td
>
-
</
tr
>
-
</
table
>
-
<
br
/>
-
<
br
/>
-
<
asp:Label
ID
=
"Label2"
runat
=
"server"
Text
=
""
></
asp:Label
>
-
</
div
>
-
</
form
>
</
body
>
</
html
>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
meta
charset
=
"utf-8"
/>
<
title
></
title
>
<
style
>
td {
padding: 5px;
}
.b {
text-align: right;
}
.c {
text-align: center;
}
</
style
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
table
>
<
tr
>
<
td
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text
=
"E-MAIL"
></
asp:Label
>
</
td
>
<
td
>
<
asp:TextBox
ID
=
"TextBox1"
runat
=
"server"
></
asp:TextBox
>
</
td
>
<
td
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ErrorMessage
=
"RequiredFieldValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
>Заполните поле</
asp:RequiredFieldValidator
>
</
td
>
<
td
>
<
asp:RegularExpressionValidator
ID
=
"RegularExpressionValidator1"
runat
=
"server"
ErrorMessage
=
"RegularExpressionValidator"
ControlToValidate
=
"TextBox1"
ForeColor
=
"Red"
ValidationExpression
=
"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
>Неверно</
asp:RegularExpressionValidator
>
</
td
>
</
tr
>
<
tr
>
<
td
>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"OK"
OnClick
=
"Button1_Click"
/>
</
td
>
<
td
></
td
>
<
td
></
td
>
<
td
></
td
>
</
tr
>
</
table
>
<
br
/>
<
br
/>
<
asp:Label
ID
=
"Label2"
runat
=
"server"
Text
=
""
></
asp:Label
>
</
div
>
</
form
>
</
body
>
</
html
>
Default.aspx.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
_Default : System.Web.UI.Page {
-
protected
void
Page_Load(
object
sender, EventArgs e) {
-
/*---При возникновении ошибки---*/
-
/*В WebForms для режима UnobtrusiveValidationMode требуется сопоставление
-
ScriptResourceMapping для "jquery". Добавьте сопоставление
-
ScriptResourceMapping с именем jquery (с учетом регистра)*/
-
/*---добавьте код---*/
-
Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
-
/*---или добавьте строку в файл Web.config---*/
-
/*
-
<appSettings>
-
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
-
</appSettings>
-
*/
-
}
-
protected
void
Button1_Click(
object
sender, EventArgs e) {
-
if
(Page.IsValid) {
-
Label2.Text =
"Введенный e-mail "
+ TextBox1.Text;
-
}
-
}
}
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
public
partial
class
_Default : System.Web.UI.Page {
protected
void
Page_Load(
object
sender, EventArgs e) {
/*---При возникновении ошибки---*/
/*В WebForms для режима UnobtrusiveValidationMode требуется сопоставление
ScriptResourceMapping для "jquery". Добавьте сопоставление
ScriptResourceMapping с именем jquery (с учетом регистра)*/
/*---добавьте код---*/
Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
/*---или добавьте строку в файл Web.config---*/
/*
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
*/
}
protected
void
Button1_Click(
object
sender, EventArgs e) {
if
(Page.IsValid) {
Label2.Text =
"Введенный e-mail "
+ TextBox1.Text;
}
}
}