Validate Dropdownlist using javascript in ASP.NET

No.of Views2651
Bookmarked0 times
Downloads 
Votes0
By  Troy   On  15 Feb 2010 23:02:08
Tag : ASP.NET , Validation Controls
Validate Dropdownlist using javascript in ASP.NET
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction


Here we have to discuss how to validate a Dropdownlist using javascript.we can also validate a dropdownlist using RequireFieldValidator.



Language

Java script



Implementation

Validator using Client Side :-

JavaScript

{codecitation class="brush: javascript; gutter: true;" width="500px"}

function CountryCheck(source, args)
{
var ddlCountry = document.getElementById(””);
var country =ddlCountry.options[ddlCountry.selectedIndex].value;
if (country == “0″)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

{/codecitation}


Html Code

{codecitation class="brush: html; gutter: true;" width="500px"}

<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select Country" Selected="True"></asp:ListItem>
<asp:ListItem Text="Japan"></asp:ListItem>
<asp:ListItem Text="USA"></asp:ListItem>
<asp:ListItem Text="Egypt"></asp:ListItem>
<asp:ListItem Text="UK"></asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="DropDownList1" runat="server" ErrorMessage="Please Select Country" ClientValidationFunction=”CountryCheck”></asp:CustomValidator>
</td>


{/codecitation}

Conclusion

For this we use CustomValidator so write a function to validate a Dropdownlist and pass the function to Clientvalidationfunction property of Customvalidator.

Thank you

Venkat


 
Sign Up to vote for this article
 
About Author
 
Troy
Occupation-Not Provided
Company-Not Provided
Member Type-Junior
Location-Not Provided
Joined date-10 Jun 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top