Resolving a problem where the CustomValidator control cannot verify that the control value is empty

Source: Internet
Author: User
Tags commit empty
Resolution | control | Issues on the Forum see the question http://community.csdn.net/Expert/topic/3772/3772658.xml?temp= the CustomValidator control verifies that the control value is empty. 9461634, just as I have encountered, the solution is not to specify the CustomValidator control of the ControlToValidate, so that when the submission back to verify
, the code is as follows:
<%@ Page language= "C #" autoeventwireup= "True"%>

<body>

<form runat= "Server" id= "Form1" >
<asp:textbox id= "Text1"
runat= "Server"/>

<asp:customvalidator id= "CustomValidator1"
clientvalidationfunction= "Clientvalidate"
Display= "Static"
Errormessage= "Can't be empty!"
Forecolor= "Green"
Font-name= "Verdana"
Font-size= "10pt"
runat= "Server"/>
<br/>
<asp:button id= "Button1"
text= "Validate"
runat= "Server"/>
</form>

</body>

<script language= "JavaScript" >
<!--
function clientvalidate (sender, args)
{
if (form1.text1.value.length<=0)
{
Args. IsValid = false;
}else{
Args. IsValid = true;
}
}
-->
</script>

Problem Analysis:
Similar to all other validation controls (except RequiredFieldValidator), CustomValidator is considered valid if the input field is blank. So the CustomValidator has no way to verify null values, so that the ControlToValidate is empty, the client function always fires once each time it tries to commit, which enables the validation of the control to be empty at commit time
The following information is excerpted from msdn:http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/cpref/html/ Frlrfsystemwebuiwebcontrolscustomvalidatorclasstopic.asp


Note If the input control is empty, no validation functions are invoked and the validation succeeds. Use the RequiredFieldValidator control to require the user to enter data in the input control.
You can use the CustomValidator control without setting the ControlToValidate property. This situation typically occurs when you validate multiple input controls, or when you validate an input control that cannot be used with a validation control, such as a CheckBox control. In this case, the Value property of the event handler that is passed to the ServerValidate event and the arguments parameter of the client-side validation function will always contain an empty string (""). However, these validation functions will still be invoked when appropriate to determine the validity of the server and the client. To access the validated value, you must programmatically reference the input control that you want to validate, and then retrieve the value from the corresponding property. For example, to verify the CheckBox control on the server, do not set the ControlToValidate property of the validation control and use the following code for the handler for the ServerValidate event.


Use:
You may say that since it is not empty, why not use RequiredFieldValidator, of course, for a reason, for example, I have a check box, I want to implement only select this check box to request a text box is not empty, so I can use the following code:
<%@ Page language= "C #" autoeventwireup= "True"%>

<body>

<form runat= "Server" id= "Form1" >
<asp:checkbox id= "CheckBox1" runat= "Server" ></asp:CheckBox>
<asp:textbox id= "Text1"
runat= "Server"/>

<asp:customvalidator id= "CustomValidator1"
clientvalidationfunction= "Clientvalidate"
Display= "Static"
Errormessage= "Can't be empty!"
Forecolor= "Green"
Font-name= "Verdana"
Font-size= "10pt"
runat= "Server"/>
<br/>
<asp:button id= "Button1"
text= "Validate"
runat= "Server"/>
</form>

</body>

<script language= "JavaScript" >
<!--
function clientvalidate (sender, args)
{
if (form1.checkbox1.checked&&form1.text1.value.length<=0)
{
Args. IsValid = false;
}else{
Args. IsValid = true;
}
}
-->
</script>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.