Validation event for validation on the client side asp net

I wonder if there is a way to connect a custom function to asp net client-side validation, so every time the validation is triggered by any control, I can do some magic on the client-side user interface

I am looking for a general approach to catching an onvalidating event page without setting it on every control that causes a postback

Thanks guys,

Edit:

I finished this function: (thanks @Kirk)

$(document).ready(function () {
    $('form').submit(function () {
        if (typeof Page_Validators != 'undefined') {
            var errors = '';
            $.each(Page_Validators, function () {
                if (!this.isvalid) {
                    errors += this.errormessage + '\r\n';
                }
            });
            if (errors.length > 0) {
                Alert(errors);
            }
        }
    });    
}); 
+5
source share
2 answers

To do something like this, you can put the OnClientClick event in a submit button or just a general form submit event.

Client Validation Object Model . . , , , . http://msdn.microsoft.com/en-us/library/yb52a4x0.aspx#ClientSideValidation_ClientValidationObjectModel.

isvalid.

<asp:Label id="lblZip" runat="server" Text="Zip Code:" />
<asp:TextBox id="txtZip" runat="server" /></asp:TextBox>
<asp:RegularExpressionValidator id="valZip" runat="server"
   ControlToValidate="txtZip"
   ErrorMessage="Invalid Zip Code" 
   ValidationExpression="[0-9]{5}" />

<script language=javascript>
// Call this function to do something
function txtZipOnChange() {
   // Do nothing if client validation is not active
   if (typeof(Page_Validators) == "undefined")  return;
       // Change the color of the label
       lblZip.style.color = valZip.isvalid ? "Black" : "Red";
}
</script>

Page_Validators. , .

ValidatorValidate(val), , ValidatorEnable(val, enable), .

http://msdn.microsoft.com/en-us/library/aa479045.aspx#aspplusvalid_clientside .

, . , .

onClientClick JavaScript. http://msdn.microsoft.com/en-us/library/7ytf5t7k.aspx

jQuery, ClientIDMode, .

http://weblogs.asp.net/scottgu/archive/2010/03/30/cleaner-html-markup-with-asp-net-4-web-forms-client-ids-vs-2010-and-net-4-0-series.aspx.

+2
//Page_Validators is an array of validation controls in the page. 
if (Page_Validators != undefined && Page_Validators != null) 
{ 
    //Looping through the whole validation collection. 
    for(var i=0; i<Page_Validators.length; i++) 
    { 
        ValidatorEnable(Page_Validators[i]); 
        //if condition to check whether the validation was successfull or not. 
        if (!Page_Validators[i].isvalid) 
        { 
            break; 
        } 
    } 
}

//if condition to check whether the page was validated successfully. 
if(Page_IsValid) 
{ 
    alert('Success'); 
} 
else 
{ 
    alert('Failure'); 
}

.

+1

All Articles