Web Tutorials

Interview Q & A

Code Examples

Utility Tools

ASP.NET Set InitialValue Property of RequiredFieldValidator

ASP.NET InitialValue attribute is used to set default value for input control. Validation fails if value of the input control matches with the InitialValue upon losing focus. The RequiredFieldValidator control belongs to System.Web.UI.WebControls

Syntax

<asp:RequiredFieldValidator ID="rfvURL" runat="server" ErrorMessage="URL is require"  
    ControlToValidate="txtURL" CssClass="highlight" InitialValue="URL is require">
    </asp:RequiredFieldValidator> 

ASP.NET RequiredFieldValidator Control Property

PropertyDescription
ControlToValidateSpecifies control name which needs to be validate.
DisplaySpecifies behavior of the error message.
EnableClientScriptSpecifies whether or not client-side validation is enabled.
EnabledSpecifies whether or not validation control is enabled or not.
ErrorMessageSpecifies error messageto be display in validation summary.
ForeColorSpecifies error message color.
IsValidIndicate if associate control passes validation or not.
SetFocusOnErrorSet focus on associate control if validation fails.
TextSpecifies text to be display if validation fails.
ValidationGroupSpecifies validation group name.
ValidateUpdate the isvalid propery.
ValidateGetValidationPropertyDetermineing validation property of a control, if it exists.
InitialValueSpecifies initial value of input control.

ASP.NET Set InitialValue Property of RequiredFieldValidator Example

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" 
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ASP.NET Set Initial Value Using InitialValue property Example</title>
</head>
<body>
<form id="Form" runat="server">
<div style="width: 619px">
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium" 
Font-Underline="True" Text="ASP.NET Set Initial Value Using InitialValue Example"></asp:Label>
<br /><br />
<table border="0" width="500px" cellpadding="0" cellspacing="5">
<tr><td>    
<asp:Label ID="lblName" runat="server" Font-Bold="True" Font-Size="Small"   
    Text="User name:"></asp:Label>
    </td><td>
<asp:TextBox ID="txtName" runat="server" Height="22px" Width="175px" 
CssClass="highlight" 
Text="Name is require"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" runat="server" ErrorMessage="Name is require"  
    ControlToValidate="txtName" CssClass="highlight" InitialValue="Name is require">
    </asp:RequiredFieldValidator>
</td></tr>
<tr><td>    
<asp:Label ID="lblURL" runat="server" Font-Bold="True" Font-Size="Small"   
    Text="URL:"></asp:Label>
    </td><td>
<asp:TextBox ID="txtURL" runat="server" Height="22px" Width="175px" 
CssClass="highlight" 
Text="URL is require"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvURL" runat="server" ErrorMessage="URL is require"  
    ControlToValidate="txtURL" CssClass="highlight" InitialValue="URL is require">
    </asp:RequiredFieldValidator>
</td></tr>
<tr><td>
<asp:Label ID="lblPassword" runat="server" Font-Bold="True" Font-Size="Small"   
    Text="Password:"></asp:Label> 
</td><td>
<asp:TextBox ID="txtPassword" runat="server" Height="22px" TextMode="Password"   
    Width="175px" CssClass="highlight"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" 
ErrorMessage="Password is require"   
    ControlToValidate="txtPassword"
    ></asp:RequiredFieldValidator> 
</td></tr>

<tr><td colspan="2">
<asp:Button ID="btnDisabledValidation" runat="server" Font-Bold="True" Text="Validate"
Font-Size="Small" CausesValidation="true" />
</td></tr>
<tr><td colspan="2">
<asp:Label ID="lblDisplay" runat="server" Font-Bold="True" Font-Size="Small"   
    Text=""></asp:Label> 
</td></tr>
</table>   
</div> 
</form>
</body>
</html>

Above example will produce following output

RequiredFieldValidator

RegularExpressionValidator

RangValidator

CompareValidator

CustomValidator

ValidationSummary