I have learned a bit about validation controls (and they are nice). Here
is my question:
Validation controls are extremely easy to use but I find them very
restricting, which is why I never go anywhere near them... I always roll my
own client-side JavaScript validation every time...
Suppose you have a group of checkboxes and want to validate that at least
one of them is checked. Is there a control for that?
What about that at least one of a group of text boxes has an entry and
that it is a number within a particular range?
<script text="text/javascript">
function validateForm()
{
if (!document.getElementById('<%=myCheckBox1.ClientID%>').checked &&
!document.getElementById('<%=myCheckBox2.ClientID%>').checked &&
!document.getElementById('<%=myCheckBox3.ClientID%>').checked
&&)
{
alert('At least one of the checkboxes must be checked');
return false;
}
if (document.getElementById('<%=myTextBox1.ClientID%>').value.length
== 0 &&
document.getElementById('<%=myTextBox2.ClientID%>').value.length
== 0 &&
document.getElementById('<%=myTextBox3.ClientID%>').value.length
== 0 &&
{
alert('At least one of the textboxes must contain data');
return false;
}
if (document.getElementById(' said:
{
if
(!isNumeric(document.getElementById('<%=myTextBox1.ClientID%>').value)
{
alert('Textbox1 does not contain a valid number');
return false;
}
if
(parseInt(document.getElementById('<%=myTextBox1.ClientID%>').value) <= 0)
{
alert('Textbox1 must contain a valid number greater than
zero');
return false;
}
}
}
</script>
<asp:Button ID="MyButton" runat="server" OnClick="MyButton_Click"
OnClientClick="return validateForm();" />