简体   繁体   English

如果TextBox不为空,则启用RequiredFieldValidator

[英]Enable RequiredFieldValidator if TextBox is not empty

can you help me with this simple problem of mine in asp.net, 您能帮我解决asp.net中这个简单的问题吗?

I have 3 textboxes; 我有3个文本框; Txt1 Txt2 txt3 Txt1 Txt2 txt3

If txt1 is not empty, txt2 and txt3 requiredvalidator should be enabled. 如果txt1不为空,则应启用txt2和txt3 requiredvalidator。 if txt1 is empty, txt2 and txt3 requiredvalidator should not be enabled, 如果txt1为空,则不应启用txt2和txt3 requiredvalidator,

Here's the requirement, once txt1 has a value, txt2 and txt3 should be a required field. 这是要求,一旦txt1具有值,则txt2和txt3应该是必填字段。

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>

You can use CustomValidators for Txt2 txt3 , On server validation event of custom validator you can check like below 您可以为Txt2 txt3使用CustomValidators ,在自定义验证器的服务器验证事件中,您可以像下面这样检查

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM