RequiredFieldValidator in ASP.Net with Set focus on error - ASP TANMOY

Latest

Thursday, 27 April 2017

RequiredFieldValidator in ASP.Net with Set focus on error

The RequiredFieldValidator is used to ensure that the user has entered the data into input control. The RequiredFieldValidator control belongs to


System.Web.UI.WebControls



1. For Text Box

  1. <RequiredFieldValidator  
  2. id="reqFirstName" ControlToValidate="TextBox1" Text="(Required)"  
  3. SetFocusOnError="true" Runat="server" ValidationGroup="f1"></asp:RequiredFieldValidator>  

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.
RequiredFieldValidatorSpecifies initial value of input control.


2. For Dropdown List

  1. <RequiredFieldValidator  
  2. id="reqFirstName" ControlToValidate="TextBox1" Text="(Required)"  
  3. SetFocusOnError="true" Runat="server" InitialValue="--Select Service--"  ValidationGroup="f1"></asp:RequiredFieldValidator>  



3. For email address validation

  1. <asp:TextBox ID="txtEmailAddress" runat="server"  placeholder="Email here..." type="email" class="form-control"></asp:TextBox> 
  2. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Email/ Username" ControlToValidate="txtEmailAddress" ValidationGroup="v1" ForeColor="#cc0000" SetFocusOnError="true"></asp:RequiredFieldValidator>
  3. <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmailAddress" ForeColor="Red" ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"  ErrorMessage = "Invalid email address" ValidationGroup="v1" SetFocusOnError="true"/>


Here there is two control one is RequiredFieldValidator which validate empty field and another is RegularExpressionValidator which validate email address. I have use here one regular express which validate any email address.









No comments:

Post a Comment