Client validation
-
I have a form which has datagrid,checkboxes and radiobutton.Datagrid also contains checkboxes like what we see in mailbox.i want to do client side validation for checkboxes inside datagrid.If the user didnt select any checkboxes in datagrid and press delete, i have to throw an client side error.How to do this.any code snippets.I dont want checkboxs outside the datagrid to fire.
-
I have a form which has datagrid,checkboxes and radiobutton.Datagrid also contains checkboxes like what we see in mailbox.i want to do client side validation for checkboxes inside datagrid.If the user didnt select any checkboxes in datagrid and press delete, i have to throw an client side error.How to do this.any code snippets.I dont want checkboxs outside the datagrid to fire.
There are a number of examples out there that you can consult. Below is just one of them: + You first need a method running on the client-side to do the checkbox status checking:
function ValidateDeleteCheckbox(checkboxName)
{
var item = document.forms[0].elements[checkboxName];
if(item != null)
{ if (item.length != null){
for (var i=0; i<item.length; i++){
if (item[i].checked)
{
return true;
}
}
}
else if(item.checked)
{
return true;
}
}return false;
}
I assume that each row has a check box called "chkItem" for the sake of simplicity:
<input type="checkbox" name="chkItem" value=''>
+ Because you only need to check when the user presses the Delete button, so the above function is only called under such condition:
this.btnDelete.Attributes.Add("onclick", "if(!ValidateDeleteCheckbox(\"chkItem\")){ alert(\"Please tick a checkbox\"); return false;}");
In addition, you also can use a custom validator to do a checking to have a better way of displaying the error msg. And this custom validator should only be enabled when the user presses Delete:
function Delete_OnClick()
{
window.document.getElementById("CustomValidator1").enabled = true;
}function ClientValidate(source, arguments)
{
if(ValidateDeleteCheckbox("chkItem"))
{
arguments.IsValid = true
}
else
{
arguments.IsValid = false
}
window.document.getElementById("CustomValidator1").enabled = false;
}Remember to assign the ClientValidate function to the custom validator's ClientValidationFunction property, and register the Delete_OnClick function for the button Click event:
this.btnDelete.Attributes.Add("onclick", "Delete_OnClick();");