Using
Validation controls we can validate many more thing what we are using in our
application for uploading, input, output etc. validation control validate that
controls what we set for that in controltovalidate property of the validation
control. We can set multiple validation control for one control. Using
validation we can provide much functionality and less coding in dot net. It is
very useful for validation. It could be dynamic or static or we can set client
side validation or server side validation.
Using
regular validation we can set regular fields like emails, phone numbers, zip
code etc. required validation use for controls value should not be blank. Range
validation use for range we can set range limit using maximum range to minimum
range in range validation. In this validation we can create our custom
validation for some other purpose or for we have to create some custom
validation for controls.
Compare
validation use full for value of this control should must be same of that
control like password and confirm password, email and confirm email. So both values
should be same if not then compare validate fire and generate error for that
controls. We can set controltovalidate and controltocompare in this compare
validation.
Here we
will see how to set validation in file upload control for file upload file
should be like Pdf, docx, xml etc… or FileUpload should not be blank.
Step 1: Drag FileUpload control in your
webpage and Drag Required, Regular validation control also.
<asp:FileUpload ID="FileUpload1"
runat="server"
/>
<br
/>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
ErrorMessage="Required"
ControlToValidate="FileUpload1"
runat="server"
Display="Dynamic"
ForeColor="Red"
ValidationGroup="ValidGroup"
/>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1"
ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.doc|.docx|.pdf)$"
ControlToValidate="FileUpload1"
runat="server"
ForeColor="Red"
ErrorMessage="Please
select a valid Word or PDF File file." Display="Dynamic" ValidationGroup="ValidGroup" />
Step 2: Set both Controls Controltovalidate
Property to FileUpload Control Name Property.
ControlToValidate="FileUpload1"
Step 3: Now in Regular Expression Validation
Set Regular Expression for Upload file should be in this Expression.
ValidationExpression="([a-zA-Z0-9\s_\\.\-:])+(.doc|.docx|.pdf)$"
Step 4: Set Display Property of both Controls
to Dynamic. And you can Create Validation Group for validation.
Display="Dynamic",
ValidationGroup="ValidGroup"
Step 5: run your Webpage and see for
validation.
Step 6: Here I have Give more validation
Example for different validation controls.
Examples:
<div>
Password
<asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Required"
Display="Dynamic"
ForeColor="Red"
ControlToValidate="TextBox1"
ValidationGroup="ValidGroup"></asp:RequiredFieldValidator>
<br />
ConfirmPassword
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="Password
Must be Same" Display="Dynamic" ForeColor="Red" ControlToCompare="TextBox1" ControlToValidate="TextBox2" ValidationGroup="ValidGroup"></asp:CompareValidator>
<br />
Range
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Range Should Must be 1 to 10" Display="Dynamic"
ForeColor="Red"
ControlToValidate="TextBox3"
MaximumValue="10"
MinimumValue="1"
ValidationGroup="ValidGroup"></asp:RangeValidator>
<br />
Email
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server"
Display="Dynamic"
ForeColor="Red"
ValidationGroup="ValidGroup" ErrorMessage="Required" ControlToValidate="TextBox4">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ErrorMessage="Not Valid Email" Display="Dynamic" ForeColor="Red" ValidationGroup="ValidGroup" ControlToValidate="TextBox4" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator>
<br />
<asp:Button ID="Button1" Text="Submit" runat="server" Width="72px" ValidationGroup="ValidGroup" />
</div>
Thanks for comments.....