Client side action after validation

Last post 09-21-2007 12:56 PM by PLBlum. 7 replies.

Sort Posts:

  • Client side action after validation

    07-23-2007, 9:46 AM
    • Loading...
    • grhm
    • Joined on 02-28-2007, 4:55 PM
    • Posts 20

    Hi guys.

    Somebody knows how can I trap the triggering of a validator in client side?
    I have a textbox with a RequiredFieldValidator attached to it. When the textbox loses focus the validation is performed. How can I do some client side action after the validation? Like displaying a nice error box....

     Thanks in advance,
    Gustavo

  • Re: Client side action after validation

    07-23-2007, 9:58 AM

     Use the ValidationSummary control? 

    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.aspx

    You might also look into the ajax toolbox ValidationCallout - http://ajax.asp.net/ajaxtoolkit/ValidatorCallout/ValidatorCallout.aspx

     

     

  • Re: Client side action after validation

    07-23-2007, 10:03 AM
    • Loading...
    • sanjayd
    • Joined on 07-10-2007, 11:09 AM
    • Posts 55

     

    You can use the Java script validation and in the alert error message you can write the color and other things how the error message is display.
    If this will help you do not forget to "Mark as Answer"

    Sanjay D.
  • Re: Client side action after validation

    07-23-2007, 1:49 PM
    • Loading...
    • PLBlum
    • Joined on 06-28-2002, 9:20 AM
    • Boston, MA
    • Posts 5,172
    • TrustedFriends-MVPs

    Hi Gustavo,

    The ASP.NET validators have many limitations. It does not provide a way to display an error message immediately after a textbox is changed. It does provide the following:

    * it shows the error message on the page (which is a pretty good way to get the user's attention)

    * you can set SetFocusOnError to true on the validator to move the focus back to the field

    * you can show an alert with all error messages when the user attempts to submit the page. Add the ValidationSummary control and set ShowMessageBox=true. Note that if you have any validators that demand a postback to validate, they will not get displayed in the messagebox.

    I wrote a commercial eplacement to the ASP.NET validators that goes much further, such as offering an alert when the textbox is changed and changing the field's style. You can learn more here.

    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Client side action after validation

    07-24-2007, 4:42 AM
    • Loading...
    • grhm
    • Joined on 02-28-2007, 4:55 PM
    • Posts 20

    Thank you for the answers.

    ValidationSummary doesn't solve the problem because what I'm looking for is display an error message in a nice box when the user leaves the fields.

    The problem arised because of CalloutExtender. Attach them to 50 textboxes in a page and you can see the performance of the app going down the drain...

  • Re: Client side action after validation

    08-02-2007, 6:08 AM
    Answer
    • Loading...
    • grhm
    • Joined on 02-28-2007, 4:55 PM
    • Posts 20

    Eventually I got this sorted out.

    This is what I've done:
    1 - Open WebResource.axd to the address of the 2 javascript used for validation. The adress can be found doing a view source in a page with validators.
    You can find a nice walkthrough in http://support.microsoft.com/kb/931762

    2 - Go to Webforms.js copied in the previous step and change the AllValidatorsValid(validators) method to perform the code. In my case I call a javascript method to display the error message.
    The method looks like this now:

    function AllValidatorsValid(validators) {
        if ((typeof(validators) != "undefined") && (validators != null)) {
            var i;
            for (i = 0; i < validators.length; i++)
            {
                if (!validators[i].isvalid)
                {
              
      
    if(showValidationBox)
              {
               var text = '<span><b>ERROR:</b></span><br/><span>' + validators[i].errormessage + '</span>'
               showValidationBox (document.getElementById(validators[i].controltovalidate).id, 'ErrorBox', 'ErrorBoxGhost', null, null, text, 200, 75)
                    }
                   
                    return false;
                }
                else
                {
                 hideValidationBox('ErrorBox', 'ErrorBoxGhost')
                }
            }
        }

        return true;
    }

     

    And that's that! One problem... I couldn't get this to work in pages with Microsoft Ajax, because this framework needs it own implementation of Webforms.js and I couldn't get it to search for the file where I want...

    If you need any more details just tell me something.

    Regards

  • Re: Client side action after validation

    09-20-2007, 9:19 PM
    • Loading...
    • Dimebrain
    • Joined on 07-13-2007, 11:41 PM
    • Posts 72

    I have the reverse need. I have a TextBox with a RequireFieldValidator, as well as a Button. I only want validation to fire when the button is pressed, and not when the TextBox loses focus. TextBox, RequiredFieldValidator, and Button are all in their own ValidationGroup. CausesValidation is set to false for the TextBox, and true for the Button. 

    Some day I'll be able to answer more questions than I ask.
  • Re: Client side action after validation

    09-21-2007, 12:56 PM
    • Loading...
    • PLBlum
    • Joined on 06-28-2002, 9:20 AM
    • Boston, MA
    • Posts 5,172
    • TrustedFriends-MVPs

    You want to disable the "onchange" event from firing a validator but allow the button to fire it. That's doable although it demands a postback.

    1. Set the EnableClientScript property to false on the validator.

    2. Keep the button's CausesValidation set to true.

    3. If you have multiple validation groups, set them up on the buttons and associated validators.

    4. *ALWAYS* test Page.IsValid is true in the server side postback event handler (Click method for your button).

    FWIW: There are so many limitations to the ASP.NET validators. The inability to do this on the client-side is one. My commercial replacement to the ASP.NET validators addresses many limitations including this one. Each validator has a property called EventsThatValidate. Set it to OnSubmit and you will have client-side validation when the buttons are pressed.

    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
Page 1 of 1 (8 items)
Microsoft Communities
Page view counter