Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts

    how to return a true or false value from a function to disable automatic postback

    Hi All,

    This is in ASP.Net

    Im creating a task loggin system and this allows the user to raise tasks and update them, im using javascript for validation and if the fields are empty it then changes the fields back ground to Red which is fine but then it goes on to update the data in SQL which i dont want, any suggestions or how i can achieve no post back is the result from the function is False (for fields in error)

    Html Code where im calling the function

    Code:
    <asp:button runat="server" id="btnChangeCommentsAuth" CSSclass="RaiseButton" OnClientClick="CommentsCheck('MainDisplayContentChange_txtAuthCommentsArea');" PostBackUrl="~/RaiseTaskChange.aspx" Text="Auth" />
    Javascript function

    Code:
    function CommentsCheck(comments) 
    {
        var com;
        var result = new boolean();
        com = document.getElementById(comments);
        if (document.getElementById(comments).value == '') {
            com.style.backgroundColor = "#B20635";
            result = 0;
        }
        else 
        {
        result = 1
        }
    }
    Thanks in Advance look forward to your reply.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    First, change your onclick function call as shown below -
    Code:
    OnClientClick="return CommentsCheck('MainDisplayContentChange_txtAuthCommentsArea');"
    Then, change your result value from 0,1 to false,true as shown below
    Code:
    function CommentsCheck(comments) 
    {
        var com;
        var result = false;
        com = document.getElementById(comments);
        if (com.value == '') {
            com.style.backgroundColor = "#B20635";
            result = false;
        }
        else 
        {
        result = true
        }
        return result;
    }
    Hope it may help you out..

    Thanks & Regards,
    Niral Soni

  • #3
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hi Niral Soni,

    Thanks for the information, but im getting an error saying Boolean is underfined?

    Any idea?

    Im new to javascript

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    It probably says 'boolean' is undefined, not 'Boolean'.
    You don't need the Boolean constructor:
    Code:
    function CommentsCheck(comments) 
    {
        var com =  document.getElementById( comments ),
            result = /\S/.test( com.value );
        
        com.style.backgroundColor = ( result ? "" : "#B20635" );
    
        return result;
    }

  • Users who have thanked Logic Ali for this post:

    Scott.Atkinson (09-14-2011)

  • #5
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Logic Ali thank you soooo much i am now going to use this nifty bit of jscript through out my project thank you

  • #6
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Logic Ali,

    Sorry for this but last question is there a way i can mod the jscript you have provided to do the following,

    i want it to check the field i pass in and if there is a error then return false and change the back color to red which is what you have provided, but if the field in check is fine then carry on to the next function similar to an

    if
    check the field to see if its blank
    if it is then return false

    else
    Call function


    Any help or a snippet will be highly appreciated....

    thanks for your time

  • #7
    New Coder
    Join Date
    Sep 2011
    Posts
    34
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Not to worry i have figured this one out for my self thanks again for the snippet.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •