Question

can you help me with this simple problem of mine in asp.net,

I have 3 textboxes; Txt1 Txt2 txt3

If txt1 is not empty, txt2 and txt3 requiredvalidator should be enabled. if txt1 is empty, txt2 and txt3 requiredvalidator should not be enabled,

Here's the requirement, once txt1 has a value, txt2 and txt3 should be a required field.

can someone help me with this??

Thank you so much.


Can somebody check this code for me? THANK YOU so much

<script type="text/javascript" language="javascript">
    function FatherClientValidate(oSrc, args) {
        var textBox = document.getElementById('<%=FatherName.ClientID%>');
        if (textBox.value != '') {

            var ctrlid = oSrc.id;
            var validatorid = document.getElementById(ctrlid);
            ctrlid = validatorid.controltovalidate;
            document.getElementById(ctrlid).style.backgroundColor = "#ff0000";
            args.IsValid = true;
        }
        else {
            var ctrlid = oSrc.id;
            var validatorid = document.getElementById(ctrlid);
            ctrlid = validatorid.controltovalidate;
            document.getElementById(ctrlid).style.backgroundColor = "White";
            args.IsValid = false;
        }
    }
</script>
Was it helpful?

Solution

You can use CustomValidators for Txt2 txt3 , On server validation event of custom validator you can check like below

void ServerValidation (object source, ServerValidateEventArgs args)
 {
    if (!string.IsNullOrEmpty(Txt1.Text))
       args.IsValid = !string.IsNullOrEmpty(args.Value);
 }

In client side validation

<script language="javascript"> 
   function ClientValidate(source, arguments)
   {
        var textBox = document.getElementById('<%=Txt1.ClientID%>');
        if (textBox.value !== "" ){
            arguments.IsValid = (args.value !== "");
        } else {
            arguments.IsValid = false;
        }
   }
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top