Tuesday, December 21, 2010

Custom Validator With ClientValidation Property

//Check whether the either one of text box or drop down list is selected using JQuery */

//Step 1 - Javascript Code
function CheckProjectEntered(source, arguments) {
try {

var ddlProject = $("select[id*='ddlProject']");
var txtProjectTitle = $("input[id*='txtProjectTitle']");

if ($(ddlProject).get(0).selectedIndex == 0 && $(txtProjectTitle).val().length == 0)
arguments.IsValid = false;
else
arguments.IsValid = true;

} catch (e) {
}
}

//Step 2:
< id="vsProjects" runat="server" cssclass="error">

//Step 3:

//DropDownList Control
< id="ddlProject" runat="server" width="200px" tabindex="1">
< / asp:DropDownList>


< id="cvProjectTypeId" runat="server" display="None" clientvalidationfunction="CheckProjectEntered" errormessage="Select or Enter a Project name!" text=" " validateemptytext="True">< / asp:CustomValidator >

//Step 4:
//TextBox
< id="txtProjectTitle" runat="server" width="200px" tabindex="2" maxlength="20">< / asp : TextBox >


< id="cvCreateProject" runat="server" display="None" clientvalidationfunction="CheckProjectEntered" errormessage="Select or Enter a Project name!" text=" " validationgroup="ProjectExport" validateemptytext="True"> < / asp:CustomValidator >

//Step 5:
< id="btnExport" runat="server" text="Export My Project" cssclass="button" causesvalidation="true" tabindex="3">
< id="btnCancel" runat="server" text="Cancel" cssclass="button" causesvalidation="false" onclientclick="$find('mpeEnterProject').hide();" tabindex="4">

//Key points to note
1. The controltovalidate property should not be used.

2. CausesValidation should be true for the control which is going to be validation and should be false for the control which has the cancel option.

No comments:

Post a Comment