validation summary and javascript

Last post 07-04-2009 10:49 AM by deepthoughts. 3 replies.

Sort Posts:

  • validation summary and javascript

    07-03-2009, 10:24 AM
    • Member
      1 point Member
    • saurabh.15in
    • Member since 10-01-2007, 5:52 AM
    • Posts 32

    Hi

    on my save button..i am validating some fields using required field validator and some expression using javascript which returns true if succed.

    If the javascript returns true then my page is getting post back even if some fields are showing required field validator fails.

     

    Please suggest,

    Thanks,

    Saurabh

  • Re: validation summary and javascript

    07-03-2009, 11:02 PM
    • Contributor
      5,012 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 738

    Yes it will happen. Because when you return true in the onclick event of the save button, it will cause the form to be posted back to the server.

    So if you want to do some custom validations, you should go for asp:CustomValidator.

    Lets suppose you've a textbox named "txt1" and you want to put two validation controls on it. One is a RequiredFieldValidator and other a CustomValidator. Our CustomValidator will check if the length of the text is greater than 5. If it is not, an error message will be shown.

     

        <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt1" ErrorMessage="Please enter text" ValidationGroup="VG"></asp:RequiredFieldValidator>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txt1" ClientValidationFunction="ValidateMe" ErrorMessage="Text length must not be less than 5." ValidationGroup="VG"></asp:CustomValidator>
        
        <asp:Button ID="btn1" runat="server" Text="Submit" ValidationGroup="VG" CausesValidation="true"  />


     

    and the javascript method for doing the validation will be

     

        <script type="text/javascript">
            function ValidateMe(sender, args) {
                if (args.Value.length < 5) {
                    args.IsValid = false;
                    return;
                }
                args.IsValid = true;
        }
        </script>


     

    Please refer to following link for more details.

    http://www.4guysfromrolla.com/articles/073102-1.aspx

     

    Hope it helps.

    MARK AS ANSWER if it helps
  • Re: validation summary and javascript

    07-04-2009, 10:39 AM
    Answer
    • Member
      1 point Member
    • saurabh.15in
    • Member since 10-01-2007, 5:52 AM
    • Posts 32

     Thanks a lot...it solved my problem.....

  • Re: validation summary and javascript

    07-04-2009, 10:49 AM
    • Contributor
      5,012 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 9:55 AM
    • Posts 738

    If it solved your problem then please mark the post as answer :-)

    All the best

    MARK AS ANSWER if it helps
Page 1 of 1 (4 items)