return false alternative

Discussion in 'Javascript' started by Peter Afonin, Mar 20, 2007.

  1. Peter Afonin

    Peter Afonin Guest

    Hello,

    I'm using Javascript in ASP.NET application to check whether at least one
    checkbox in datagrid has been checked. If validation fails, the user gets a
    warning. If he clicks OK, the form is submitted, if Cancel - nothing should
    happen.

    The ASP.NET code looks like this:

    btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    & "))" _
    & "{ confirm('You have not selected any DRs. Are you sure you want to submit
    change?');return false}")

    All validation is in sClientSideValidate.

    The problem is with "return false". ASP.NET doesn't like the word "return".
    If I use it - all my ASP.NET validation controls don't work. For instance,
    if I use "return confirm" - I have a problem, if I use just "confirm" - I
    have no problem.

    Same with "return false". But in this case I don't know what could I use
    instead of "return false". I have to use something to cancel form submission
    if the user clicks "Cancel".

    I'm not an expert in Javascript, so I hope someone could tell me - what
    could I use instead of "return false" to get the same results?

    I would appreciate your help.

    Thank you,


    --
    Peter Afonin
    Peter Afonin, Mar 20, 2007
    #1
    1. Advertising

  2. Peter Afonin

    Tom Cole Guest

    On Mar 19, 9:05 pm, "Peter Afonin" <> wrote:
    > Hello,
    >
    > I'm using Javascript in ASP.NET application to check whether at least one
    > checkbox in datagrid has been checked. If validation fails, the user gets a
    > warning. If he clicks OK, the form is submitted, if Cancel - nothing should
    > happen.
    >
    > The ASP.NET code looks like this:
    >
    > btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    > & "))" _
    > & "{ confirm('You have not selected any DRs. Are you sure you want to submit
    > change?');return false}")
    >
    > All validation is in sClientSideValidate.
    >
    > The problem is with "return false". ASP.NET doesn't like the word "return".
    > If I use it - all my ASP.NET validation controls don't work. For instance,
    > if I use "return confirm" - I have a problem, if I use just "confirm" - I
    > have no problem.
    >
    > Same with "return false". But in this case I don't know what could I use
    > instead of "return false". I have to use something to cancel form submission
    > if the user clicks "Cancel".
    >
    > I'm not an expert in Javascript, so I hope someone could tell me - what
    > could I use instead of "return false" to get the same results?
    >
    > I would appreciate your help.
    >
    > Thank you,
    >
    > --
    > Peter Afonin


    First of all your closing brace is not in the proper place, it should
    be before the return statement. But you can do even better than that.
    The confirm box returns the users selection so you can check for true
    or false and return it:

    I have no experience with ASP (I'm a JSP developer) but it should look
    something like this:

    btnSubmit.Attributes.Add("onclick", "function() = { if (! " &
    sClientSideValidate.ToString & ") { return confirm('You have not
    selected any DRs. Are you sure you want to submit change?'); } }");

    HTH.
    Tom Cole, Mar 20, 2007
    #2
    1. Advertising

  3. Peter Afonin

    Peter Guest

    Thank you very much.

    The function you wrote looks perfectly correct to me. However, I'm
    getting error "Expected: '{'", and I cannot figure out where it could
    be expected.

    This is how this function looks in the source:

    <input type="submit" name="btnSubmit" value="Submit"
    onclick="function() = { if (! dgReport__ctl2_chkConf.checked ||
    dgReport__ctl3_chkConf.checked ||dgReport__ctl4_chkConf.checked ||
    dgReport__ctl5_chkConf.checked ||dgReport__ctl6_chkConf.checked)
    { return confirm('You have not selected any DRs. Are you sure you want
    to submit change?'); } }if (typeof(Page_ClientValidate) == 'function')
    Page_ClientValidate(); " language="javascript" id="btnSubmit"
    class="button" />

    Thank you,

    Peter

    On Mar 20, 6:49 am, "Tom Cole" <> wrote:
    > On Mar 19, 9:05 pm, "PeterAfonin" <> wrote:
    >
    >
    >
    >
    >
    > > Hello,

    >
    > > I'm using Javascript in ASP.NET application to check whether at least one
    > > checkbox in datagrid has been checked. If validation fails, the user gets a
    > > warning. If he clicks OK, the form is submitted, if Cancel - nothing should
    > > happen.

    >
    > > The ASP.NET code looks like this:

    >
    > > btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    > > & "))" _
    > > & "{ confirm('You have not selected any DRs. Are you sure you want to submit
    > > change?');return false}")

    >
    > > All validation is in sClientSideValidate.

    >
    > > The problem is with "return false". ASP.NET doesn't like the word "return".
    > > If I use it - all my ASP.NET validation controls don't work. For instance,
    > > if I use "return confirm" - I have a problem, if I use just "confirm" - I
    > > have no problem.

    >
    > > Same with "return false". But in this case I don't know what could I use
    > > instead of "return false". I have to use something to cancel form submission
    > > if the user clicks "Cancel".

    >
    > > I'm not an expert in Javascript, so I hope someone could tell me - what
    > > could I use instead of "return false" to get the same results?

    >
    > > I would appreciate your help.

    >
    > > Thank you,

    >
    > > --
    > >PeterAfonin

    >
    > First of all your closing brace is not in the proper place, it should
    > be before the return statement. But you can do even better than that.
    > The confirm box returns the users selection so you can check for true
    > or false and return it:
    >
    > I have no experience with ASP (I'm a JSP developer) but it should look
    > something like this:
    >
    > btnSubmit.Attributes.Add("onclick", "function() = { if (! " &
    > sClientSideValidate.ToString & ") { return confirm('You have not
    > selected any DRs. Are you sure you want to submit change?'); } }");
    >
    > HTH.- Hide quoted text -
    >
    > - Show quoted text -
    Peter, Mar 20, 2007
    #3
  4. Peter Afonin

    Peter Guest

    And one more thing I'd like to mention. I'm afraid this code will also
    never work for me due to the same statement "return". This statement
    breaks all further code execution, that's why my other validation
    controls don't work. Is there a way to avoid it?

    Thanks again,

    Peter

    On Mar 20, 6:49 am, "Tom Cole" <> wrote:
    > On Mar 19, 9:05 pm, "PeterAfonin" <> wrote:
    >
    >
    >
    >
    >
    > > Hello,

    >
    > > I'm using Javascript in ASP.NET application to check whether at least one
    > > checkbox in datagrid has been checked. If validation fails, the user gets a
    > > warning. If he clicks OK, the form is submitted, if Cancel - nothing should
    > > happen.

    >
    > > The ASP.NET code looks like this:

    >
    > > btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
    > > & "))" _
    > > & "{ confirm('You have not selected any DRs. Are you sure you want to submit
    > > change?');return false}")

    >
    > > All validation is in sClientSideValidate.

    >
    > > The problem is with "return false". ASP.NET doesn't like the word "return".
    > > If I use it - all my ASP.NET validation controls don't work. For instance,
    > > if I use "return confirm" - I have a problem, if I use just "confirm" - I
    > > have no problem.

    >
    > > Same with "return false". But in this case I don't know what could I use
    > > instead of "return false". I have to use something to cancel form submission
    > > if the user clicks "Cancel".

    >
    > > I'm not an expert in Javascript, so I hope someone could tell me - what
    > > could I use instead of "return false" to get the same results?

    >
    > > I would appreciate your help.

    >
    > > Thank you,

    >
    > > --
    > >PeterAfonin

    >
    > First of all your closing brace is not in the proper place, it should
    > be before the return statement. But you can do even better than that.
    > The confirm box returns the users selection so you can check for true
    > or false and return it:
    >
    > I have no experience with ASP (I'm a JSP developer) but it should look
    > something like this:
    >
    > btnSubmit.Attributes.Add("onclick", "function() = { if (! " &
    > sClientSideValidate.ToString & ") { return confirm('You have not
    > selected any DRs. Are you sure you want to submit change?'); } }");
    >
    > HTH.- Hide quoted text -
    >
    > - Show quoted text -
    Peter, Mar 20, 2007
    #4
  5. Peter wrote:
    > And one more thing I'd like to mention. I'm afraid this code
    > will also never work for me due to the same statement "return".
    > This statement breaks all further code execution, that's why my
    > other validation controls don't work. Is there a way to avoid
    > it?

    <snip>

    It is hard to believe that ASP.NET could really be as bad as you
    have just described. However, as you are constructing your
    javascript code from string values in another leagues couldn't
    you disguise the "return" keyword? In javascript we could do:-

    "ret"+"urn false;"

    - or:-

    "\u0072eturn false;" //replacing the r with its unicode escape sequnce.

    - for example, thought there would be no need in javascript.

    Richard.
    Richard Cornford, Mar 20, 2007
    #5
  6. Peter Afonin

    Peter Guest

    There is nothing bad about ASP.NET. "return" stops any further
    Javascript code execution, added to the button "onclick" event. That's
    why any further client side validation is not possible. It's all
    understandable. So playing with the characters won't help here.

    Thanks,

    Peter



    On Mar 20, 3:36 pm, "Richard Cornford" <>
    wrote:
    > Peterwrote:
    > > And one more thing I'd like to mention. I'm afraid this code
    > > will also never work for me due to the same statement "return".
    > > This statement breaks all further code execution, that's why my
    > > other validation controls don't work. Is there a way to avoid
    > > it?

    >
    > <snip>
    >
    > It is hard to believe that ASP.NET could really be as bad as you
    > have just described. However, as you are constructing your
    > javascript code from string values in another leagues couldn't
    > you disguise the "return" keyword? In javascript we could do:-
    >
    > "ret"+"urn false;"
    >
    > - or:-
    >
    > "\u0072eturn false;" //replacing the r with its unicode escape sequnce.
    >
    > - for example, thought there would be no need in javascript.
    >
    > Richard.
    Peter, Mar 20, 2007
    #6
  7. "Peter" <> wrote:
    > There is nothing bad about ASP.NET.

    <snip>

    Then why did you write "ASP.NET doesn't like the word "return"."? And why are you showing us server code
    if your problem is only with the client-side code?

    Richard.
    Richard Cornford, Mar 20, 2007
    #7
  8. Peter Afonin

    Peter Afonin Guest

    I guess the word "doesn't like" wasn't the right one.

    In this particular case the javascript can be added only using the
    server-side code, because I have to itirate through the hundreds of rows of
    the datagrid. Well, maybe it's possible to use pure javascript without
    server-side code, but I don't know how. That's why I was showing how I added
    this code - using the server-side scripts. But the output in the HTML source
    is this (example):

    <input type="submit" name="btnSubmit" value="Submit"
    onclick="function() = { if (! dgReport__ctl2_chkConf.checked ||
    dgReport__ctl3_chkConf.checked ||dgReport__ctl4_chkConf.checked ||
    dgReport__ctl5_chkConf.checked ||dgReport__ctl6_chkConf.checked)
    { return confirm('You have not selected any DRs. Are you sure you want
    to submit change?'); } }if (typeof(Page_ClientValidate) == 'function')
    Page_ClientValidate(); " language="javascript" id="btnSubmit"
    class="button" />

    Peter

    "Richard Cornford" <> wrote in message
    news:etpqfi$dsr$1$...
    > "Peter" <> wrote:
    >> There is nothing bad about ASP.NET.

    > <snip>
    >
    > Then why did you write "ASP.NET doesn't like the word "return"."? And why
    > are you showing us server code if your problem is only with the
    > client-side code?
    >
    > Richard.
    Peter Afonin, Mar 21, 2007
    #8
  9. "Peter Afonin" wrote:
    >I guess the word "doesn't like" wasn't the right one.


    Attributing the attitude to ASP.NET doesn't appear to be right either.

    > In this particular case the javascript can be added only using
    > the server-side code, because I have to itirate through the
    > hundreds of rows of the datagrid. Well, maybe it's possible
    > to use pure javascript without server-side code, but I don't
    > know how.


    That would be irrelevant.

    > That's why I was showing how I added this code - using the server-side scripts.


    Yes, not knowing what you are doing will have you looking form the wrong thing in the wrong place much
    of time.

    > But the output in the HTML source is this (example):
    >
    > <input type="submit" name="btnSubmit" value="Submit"
    > onclick="function() = { if (! dgReport__ctl2_chkConf.checked ||

    ^^^^^^^^^^^^^^
    There are at least two syntax errors there, so this will never even get to the point of executing a
    return statement.

    > dgReport__ctl3_chkConf.checked ||dgReport__ctl4_chkConf.checked ||

    ^^^^^^^^^^^^^^^^^^^^^^^
    You are not attempting anything cross-browser then?

    > dgReport__ctl5_chkConf.checked ||dgReport__ctl6_chkConf.checked)
    > { return confirm('You have not selected any DRs. Are you sure you want
    > to submit change?'); } }if (typeof(Page_ClientValidate) == 'function')
    > Page_ClientValidate(); " language="javascript" id="btnSubmit"

    ^^^^^^^^^^^^^^^^^^^^
    What on earth is that bizarre attribute doing there?

    > class="button" />

    <snip>

    The logic you seem to want to implement is more like:-

    if(
    (
    !(
    (dgReport__ctl2_chkConf.checked)||
    (dgReport__ctl3_chkConf.checked)||
    (dgReport__ctl4_chkConf.checked)||
    (dgReport__ctl5_chkConf.checked)||
    (dgReport__ctl6_chkConf.checked)
    )
    )&&(
    !(
    confirm(
    'You have not selected any DRs. '+
    'Are you sure you want to submit change?'
    )
    )
    )
    ){
    return false;
    }else{
    return (
    (typeof Page_ClientValidate == 'function')?
    Page_ClientValidate():
    false
    );
    }

    - or as a single return statement:-


    return (
    (
    (
    (
    (dgReport__ctl2_chkConf.checked)||
    (dgReport__ctl3_chkConf.checked)||
    (dgReport__ctl4_chkConf.checked)||
    (dgReport__ctl5_chkConf.checked)||
    (dgReport__ctl6_chkConf.checked)
    )||(
    confirm(
    'You have not selected any DRs. '+
    'Are you sure you want to submit change?'
    )
    )
    )&&(
    (typeof Page_ClientValidate == 'function')&&
    Page_ClientValidate()
    )
    )||(
    false
    )
    );

    Richard.


    But return statements are not your problem at all.

    Richard.
    Richard Cornford, Mar 21, 2007
    #9
    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. André
    Replies:
    3
    Views:
    1,558
  2. Rajarshi

    0 == False but [] != False?

    Rajarshi, May 24, 2007, in forum: Python
    Replies:
    20
    Views:
    687
    Erik Max Francis
    May 30, 2007
  3. w i l l
    Replies:
    4
    Views:
    229
    Dan Brussee
    Jul 4, 2003
  4. Replies:
    1
    Views:
    424
    Randy Webb
    Oct 6, 2005
  5. Replies:
    10
    Views:
    299
    Thomas 'PointedEars' Lahn
    Feb 16, 2006
Loading...

Share This Page