Run client side javascript with Validation Control

Discussion in 'ASP .Net' started by goscottie@gmail.com, Oct 21, 2008.

  1. Guest

    I need to find a way to either

    1. run and check all Validation controls (in my case one
    CompareValidator) and run client side javascript function. In this
    case I'll use <asp:Button>. So if all validation passes, run custom
    javascript. Or.
    2. from my client side javascript to run Validation check and then
    perform remaining client side function. So in this case I'll use
    <input type="button">.

    So far, challenge in case 1 is how to capture validation failure and
    limit and stop further client side code execution. I had this code.

    Button_Search.Attributes.Add("onclick",
    Page.ClientScript.GetPostBackEventReference(Button_Search, "") +
    ";Search();");

    where Search() is my client side function, which gets executed with
    failed client side validation.

    In case 2, I would like to do something like... (I'm doing this with
    RegisterClientScriptBlock)

    function Search()
    {
    window.top._somevalue = 'xyz';
    if (whateverclientvalidationfuctioniscalled()) // client side
    validation function name generated by ASP.NET
    window.top.hidePopWin();
    }

    <input type="button" onclick="Search();">

    Any help appreciated.
    , Oct 21, 2008
    #1
    1. Advertising

  2. Pselus Guest

    I think you are trying to do what I do. Here is what I do:

    In the code-behind:

    // Add Submit Confirmation
    btnSubmit.Attributes.Add("onClick", "return ConfirmSubmit();");

    Then in your aspx page within script tags:

    function errorArgs()
    {
    this.IsValid = true;
    this.errorMsg = "";
    }

    function ConfirmSubmit()
    {
    var IsValid = true;
    var display = "";
    var err = new errorArgs();

    VerifyVacationDays(err); // Create your validation code in here.

    // I have multiple validation routines that I call, but I put it
    down to 1 for ease of reading

    if (err.IsValid)
    {
    return confirm('Are you sure you want to Submit?');
    }
    else
    {
    alert(err.errorMsg);
    return false;
    }
    }

    The key is the "return" in the buttons OnClick event. If it sees
    false it will not post back. If it sees true it will.
    Pselus, Oct 21, 2008
    #2
    1. Advertising

  3. Guest

    Thanks for quick response.

    So looks like you are calling your own validation functions. I would
    like to utilize already generated server-side validation. I just
    found my own answer just after posting.

    http://forums.asp.net/t/1318892.aspx

    With case 2, where...

    if (Page_ClientValidate('SearchValidation')) // that was the function
    name I was looking for. SearchValidation is the ValidationGroup name.
    window.top.hidePopWin();

    and used in server side....

    <asp:Button ID="Button_Search" runat="server" Text="Search"
    ValidationGroup="SearchValidation" OnClientClick="Search();" />

    I'm not sure <asp:Button> is completely necessary or not here but I'll
    move on. Thanks again.
    , Oct 21, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    14
    Views:
    4,074
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  2. Guadala Harry
    Replies:
    4
    Views:
    1,498
    =?Utf-8?B?aGFyaSBwcmFzYWQ=?=
    Jun 15, 2004
  3. =?Utf-8?B?dmlkeWE=?=
    Replies:
    1
    Views:
    741
    Kevin Spencer
    Jun 2, 2005
  4. Boss302
    Replies:
    0
    Views:
    1,024
    Boss302
    Nov 21, 2006
  5. Bogdan
    Replies:
    2
    Views:
    639
    Bogdan
    Jun 9, 2008
Loading...

Share This Page