Javascript disabling other validators present in the page

Last post 03-11-2004 9:34 AM by Floatsam. 4 replies.

Sort Posts:

  • Javascript disabling other validators present in the page

    03-10-2004, 10:15 AM
    • Member
      150 point Member
    • Floatsam
    • Member since 03-10-2004, 4:54 AM
    • Posts 30
    Hello,
    i have a dropdown list in a page.If the option delete is selected in the dropdownlist,i have to ask for confirmation.So i wrote a javascript function and added it as an attribute to the buttonsave.Everything is working fine but the validators like requiredfield validator that are present on the page previously are not working.
    To get over this problem,i used a customvalidator in place of the javascript function and i associated the following function as the clientvalidationfunction for that customvalidator.

    function ConfirmMessage(sender,args)
    {
    if (document.Form1.drpStatus[document.Form1.drpStatus.selectedIndex].value == "Delete")
    { var confirm = window.confirm("Do you want to delete the item permanently?");
    if (confirm)
    {
    args.IsValid = true;
    }
    else
    {
    args.IsValid = false ;
    }
    }
    }

    Now the problem with this is I have a validation summary control on the page,which is showing up if i click Cancel in the confirmation box since the condition
    args.IsValid = false is executed.
    Could anyone please suggest work around for this problem, or a solution to keep the validators working even when only the javascript function is used.

    Thanks,
    Sam.
  • Re: Javascript disabling other validators present in the page

    03-10-2004, 12:46 PM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs
    Hi Sam, welcome to these forums.

    I recommend sticking with your original design, where you hookup the confirm() command into the onclick event of your button. Simply be sure that your button also generates the necessary client-side javascript that validation would have written. To do this, setup the page without the confirm command code and use View Source to see the HTML generated on the <input type=submit onclick=[code here]/>. Steal that code and use it after your confirm() command returns true.

    If you actually don't want the validators to fire on this command, you can set CausesValidation to false on this button. It no longer adds its own onclick code.

    FYI: My product, Professional Validation And More, greatly enhances what you can do with validation, including putting up a confirm message when the page is submitted.
    --- 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: Javascript disabling other validators present in the page

    03-10-2004, 1:33 PM
    • Member
      150 point Member
    • Floatsam
    • Member since 03-10-2004, 4:54 AM
    • Posts 30

    Hello Peter,
    Got your point.But the problem is the onclick event before adding the attribute using the statement btnSave.Attributes.Add("OnClick", "Javascript:return ConfirmMessage();") looks like

    onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "

    and after adding the attribute to the button looks like

    onclick="Javascript:return ConfirmMessage();if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "

    It made no difference even if i added " if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); " statement after the return statement in the ConfirmMessage function.
    So the problem still exists- other validators not working.

    Thanks for the help,
    Sam.
  • Re: Javascript disabling other validators present in the page

    03-10-2004, 9:51 PM
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 1:20 PM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs
    So you are saying that the following, which runs the confirm command then validates, doesn't let the other validators to work when the confirm message gets OK clicked?

    btnSave.CausesValidation = false
    btnSave.Attributes.Add("onclick", "if (!ConfirmMessage()) return false; else if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); ")

    So long as Page_ClientValidate() is called, all the validators on the page will execute, either showing or hiding their error message.
    --- 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: Javascript disabling other validators present in the page

    03-11-2004, 9:30 AM
    • Member
      150 point Member
    • Floatsam
    • Member since 03-10-2004, 4:54 AM
    • Posts 30

    Peter,
    Thanks man.I was using it the wrong way.
    Thanks again,
    Sam.
Page 1 of 1 (5 items)