Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 409
KLIM8D Avatar asked Nov 14 '12 16:11

KLIM8D


People also ask

What is a validator in ASP NET?

ASP.NET - Validators. ASP.NET validation controls validate the user input data to ensure that useless, unauthenticated, or contradictory data don't get stored.

How to find the ID of the control that failed validation?

If you want to find the ID of the control that failed validation, it is possible to get that by casting the validator to a BaseValidator which exposes the ControlToValidate property. For example: For Each v As BaseValidator In Page.Validators If Not v.IsValid Then ' You can see the control to validate name and error message here.

How many types of validation controls are there in ASP NET?

There are six types of validation controls in ASP.NET RequiredFieldValidation Control CompareValidator Control RangeValidator Control RegularExpressionValidator Control CustomValidator Control ValidationSummary

How do I validate input in ASP NET Web pages?

Validating User Input. In ASP.NET Web Pages 2, you can use the Validator helper to test user input. The basic approach is to do the following: Determine which input elements (fields) you want to validate. You typically validate values in <input> elements in a form.


1 Answers

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
}

}
like image 52
MatthewMartin Avatar answered Sep 20 '22 17:09

MatthewMartin