Archive

Posts Tagged ‘custom validator’

Asp.net Custom Validator and Jquery $.ajax()

When we are using custom validation and making Ajax call using jquery library, custom validation function always returns true. We can fix this, jQuery’s $.ajax() function has a handy option: async, which when set to false does what we need – it waits for the response from the server before continuing to our success function.

For detailed description with example visit:

http://brian.dobberteen.com/other-stuff/jquery_ajax_custom_validator/

http://stackoverflow.com/questions/928309/jquery-and-asp-net-custom-validator

Advertisements

Validating Checkboxlist using CustomValidator

You might be knowing that required field validator doesn’t works with required field validator… so to accomplish that purpose we can use custom validator to validate checkbox list.

<asp:CheckBoxList ID=”chkModuleList”runat=”server” >
</asp:CheckBoxList>


1) Using ClientValidationFunction

<asp:CustomValidator runat=”server” ID=”cvmodulelist” ClientValidationFunction=”ValidateModuleList” ErrorMessage=”Please Select Atleast one Module” ></asp:CustomValidator>

function ValidateModuleList(source, args)
{
var chkListModules= document.getElementById (‘<%= chkModuleList.ClientID %>’);
var chkListinputs = chkListModules.getElementsByTagName(“input”);
for(var i=0;i<
chkListinputs .length;i++)
{
if(
chkListinputs [i].checked)
{
args.IsValid = true;
return;
}
}
args.IsValid = false;
}

2) Using OnServerValidate

<asp:CustomValidator runat=”server” ID=”cvmodulelist” OnServerValidate=”ValidateModuleList” ErrorMessage=”Please Select Atleast one Module” ></asp:CustomValidator>

private void ValidateModuleList(object sender, ServerValidateEventArgs e)
{
int cnt= 0;

for(int i=0;i<chkModuleList.Items.Count;i++)
{
if(
chkModuleList.Items[i].Selected)
{
cnt++;
}

e.IsValid = (cnt== 0) ? false : true;
}
}