RequiredFieldValidator Control
The RequiredFieldValidator control validates that the value of another control is something other than its initial value.
In terms of validation, it behaves identically to the ASP.NET System.Web.UI.WebControls.RequiredFieldValidator control.
Mobile Controls Syntax
Required properties, defaults, and code-featured elements are noted in bold type.
<mobile:RequiredFieldValidator
runat="server"
id="id"
Font-Name="fontName"
Font-Size={ NotSet|Normal|Small|Large }
Font-Bold={ NotSet|False|True }
Font-Italic="{NotSet|False|True}"
ForeColor="foregroundColor"
BackColor="backgroundColor"
Alignment="{NotSet|Left|Center|Right}"
StyleReference="styleReference"
Wrapping="{NotSet|Wrap|NoWrap}"
ControlToValidate="IdOfTargetControl"
Display="{None|Static|Dynamic}"
ErrorMessage="ErrorTextForSummary"
InitialValue="initialValueInTheControl">
innerText
</mobile:RequiredFieldValidator>
Containment Rules
The following controls can contain a RequiredFieldValidator control.
Control | Comments |
---|---|
System.Web.UI.MobileControls.Form | Can contain any number of RequiredFieldValidator controls. |
System.Web.UI.MobileControls.Panel | Can contain any number of RequiredFieldValidator controls. |
The RequiredFieldValidator control cannot contain any other controls.
Device Templates
None.
Device-Specific Behavior
When any validator is rendered, the device-specific nature of its rendering is exactly the same as the device-specific rendering of the Label control, except that the StyleReference property for the control is initially set to Error*.* When this happens, the control will use the error style in the page style sheet or, if none exists, it will use the system default style sheet.
Example
The following example requires a user to enter a number from 1 through 23. It uses both the RangeValidator and RequiredFieldValidator controls.
<%@ Page Inherits=
"System.Web.UI.MobileControls.MobilePage" Language="VB" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script language="vb" runat="server">
Protected Sub Submit_OnClick(sender As Object, e As EventArgs)
If Page.IsValid Then
ActiveForm = Form2
End If
End Sub
</script>
<mobile:Form id="Form1" runat="server">
<mobile:Label runat="server">
Please enter an integer from 1 through 23
</mobile:Label>
<mobile:TextBox id="number" runat="server"/>
<mobile:RangeValidator ControlToValidate="number"
Type="Integer"
MaximumValue="23"
MinimumValue="1"
runat="server">
Invalid number
</mobile:RangeValidator>
<mobile:RequiredFieldValidator ControlToValidate="number"
runat="server">
A number is required
</mobile:RequiredFieldValidator>
<mobile:Command runat="server" OnClick="Submit_OnClick">
Submit
</mobile:Command>
</mobile:Form>
<mobile:Form id="Form2" runat="server">
<mobile:Label runat="server">Number is submitted</mobile:Label>
</mobile:Form>
[C#]
<%@ Page Inherits=
"System.Web.UI.MobileControls.MobilePage" Language="C#" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script language="c#" runat="server">
protected void Submit_OnClick(Object sender, EventArgs e)
{
if (Page.IsValid)
{
ActiveForm = Form2;
}
}
</script>
<mobile:Form id="Form1" runat="server">
<mobile:Label runat="server">
Please enter an integer from 1 through 23
</mobile:Label>
<mobile:TextBox id="number" runat="server"/>
<mobile:RangeValidator ControlToValidate="number"
Type="Integer"
MaximumValue="23"
MinimumValue="1"
runat="server">
Invalid number
</mobile:RangeValidator>
<mobile:RequiredFieldValidator ControlToValidate="number"
runat="server">
A number is required
</mobile:RequiredFieldValidator>
<mobile:Command runat="server" OnClick="Submit_OnClick">
Submit
</mobile:Command>
</mobile:Form>
<mobile:Form id="Form2" runat="server">
<mobile:Label runat="server">Number is submitted</mobile:Label>
</mobile:Form>
See Also
RequiredFieldValidator Class | RequiredFieldValidator Class Members | RequiredFieldValidator Control