...

View Full Version : how to return a true or false value from a function to disable automatic postback



Scott.Atkinson
09-14-2011, 11:56 AM
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



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


Javascript function



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.

niralsoni
09-14-2011, 01:58 PM
First, change your onclick function call as shown below -

OnClientClick="return CommentsCheck('MainDisplayContentChange_txtAuthCommentsArea');"

Then, change your result value from 0,1 to false,true as shown below


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

Scott.Atkinson
09-14-2011, 02:08 PM
Hi Niral Soni,

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

Any idea?

Im new to javascript

Logic Ali
09-14-2011, 02:45 PM
It probably says 'boolean' is undefined, not 'Boolean'.
You don't need the Boolean constructor:
function CommentsCheck(comments)
{
var com = document.getElementById( comments ),
result = /\S/.test( com.value );

com.style.backgroundColor = ( result ? "" : "#B20635" );

return result;
}

Scott.Atkinson
09-14-2011, 02:53 PM
Logic Ali thank you soooo much i am now going to use this nifty bit of jscript through out my project thank you :)

Scott.Atkinson
09-14-2011, 03:10 PM
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

Scott.Atkinson
09-14-2011, 03:53 PM
Not to worry i have figured this one out for my self :) thanks again for the snippet.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum