ASP CustomValidator, advancing to postback after error

淺唱寂寞╮ 提交于 2019-12-02 01:46:03

问题


I have an ASP .NET page with ASP validators (Required Field, Regular Expression,...) plus java script functions for additional validation (for example, to check if second date bigger than first date, among others).

I usually do:

<script type="text/javascript">

    function validate() {
        // ...
        alert('Not valid!');
        return false;
    }    
</script>


<asp:Button ID="Button1" runat="server" Text="Add" 
            OnClientClick="return validate();" OnClick="Button1_Click" />

So, the button advances to the postback if the asp and the javascript validation both pass, and it works fine.

I’m trying out the custom validator:

<asp:CustomValidator ID="CustomValidator1" 
EnableClientScript="true" runat="server" ControlToValidate="TextBox1" 
ClientValidationFunction="validate();" >
</asp:CustomValidator> 

(also tried with ClientValidationFunction="return validate();")

But the page is continuously advancing to the postback, even after showing the “not valid” alert... Any thoughts? Thanks!


回答1:


When using a CustomValidator, the client side validation function needs to accept a source and an arguments parameter. Then, to mark the validation as failed, you set arguments.IsValid to false. Here is the MSDN page for the CustomValidator.

function validate(source, arguments) {
   // ...

   alert('Not valid!');
   arguments.IsValid=false;
}



回答2:


like @Jason wrote, when using CustomValidator client is excepting for source and arguments params. a quick sample of use both client and server side with CustomValidator.

CustomValidator use ClientValidationFunction and OnServerValidate properties:

<asp:CustomValidator ID="cvCreditCard"  runat="server" ErrorMessage="Error Message"  ControlToValidate="txtCardNumber" ClientValidationFunction="Validators.CardNumber" OnServerValidate="ValidateCreditCardValid">*</asp:CustomValidator>

Client side validation:

var Validators = {
CardNumber: function (source, clientside_arguments) {

    var valid_val = true;
    var txtCardNumber = clientside_arguments.Value; //(return the ControlToValidate value)

    //make your checks here

    clientside_arguments.IsValid = valid_val;
}}

server side validation:

protected void ValidateCreditCardValid(object sender, ServerValidateEventArgs e)
    {
       //make your checks here            
       e.IsValid = false;

    }



回答3:


I solved this problem by creating a variable:

 Boolean fieldIsValid = true;

and in the custom validating expression method I would change the value if arguments weren't true:

if(args.IsValid == false)
            {
                fieldIsValid = false;
            }
            else
            {
                fieldIsValid = true;
            }

Then, I also put that in the submit click method:

protected void submit_Click(object sender, EventArgs e)
        {
            if (fieldIsValid)
            {
                //submit my things
            }
        }


来源:https://stackoverflow.com/questions/4016843/asp-customvalidator-advancing-to-postback-after-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!