KLIM8D
KLIM8D

Reputation: 583

Find failed validators asp.net

I have a page, where I want to log every validation message which the user failed to meet the requirements the associated field.

The problem is my postback/button click never occurs (maybe because of clientside validation), and therefore the logging never takes place before the user actually got every field right (no validation errors).

The button click event method:

protected void btnNext_Click(object sender, EventArgs e)
{
    Page.Validate();
    if(Page.IsValid)
    {
        //code
    }
    else
    {
        foreach (IValidator validator in Validators)
        {
            if (!validator.IsValid)
            {
                PageValidatorErrors error = new PageValidatorErrors
                              {
                                  WebsiteID = AppState.WebsiteID,
                                  Page = Request.Url.AbsolutePath,
                                  URL = Request.Url.ToString(),
                                  UserIP = Tools.GetIP(),
                                  ErrorMessage = validator.ErrorMessage,
                                  CreatedDate = DateTime.Now
                               };
                pageValidatorErrorsRep.insert(error);
            }
        }
    }
}

Any ideas, how I could log theese messages?

Edit:

<script type="text/javascript">
    function validatePage()
    {
        if (window.Page_IsValid != true)
        {
            //Page_Validators is an array of validation controls in the page. 
            if (window.Page_Validators != undefined && window.Page_Validators != null)
            {
                //Looping through the whole validation collection. 
                for (var i = 0; i < window.Page_Validators.length; i++)
                {
                    window.ValidatorEnable(window.Page_Validators[i]);

                    //if condition to check whether the validation was successfull or not. 
                    if (!window.Page_Validators[i].isvalid)
                    {
                        var errMsg = window.Page_Validators[i].getAttribute('ErrorMessage');
                        alert(errMsg);
                    }
                }
            }
        }
    }
</script>

Upvotes: 9

Views: 5848

Answers (2)

MatthewMartin
MatthewMartin

Reputation: 33173

Here is part of the solution, you can get the validates/true false by invoking it client side:

http://razeeb.wordpress.com/2009/01/11/calling-aspnet-validators-from-javascript/

function performCheck()
{

if(Page_ClientValidate())
{

//Do something to log true/ false
}

}

Upvotes: 3

giacomelli
giacomelli

Reputation: 7417

Try to change EnableClientScript property on all validators to false. All your validations will occur only on server side.

Upvotes: 2

Related Questions