views:

1038

answers:

7

Is there any way to do the following:

validateLogin();
return false;

But actually like this..

validateLogin();

And here is the function:

function validateLogin(){
if(hi=true){
return true;
}
else{
return false
}

I want to attach this function for the event of a form being submitted, so if HI is false - i want to return false; meaning it will stop for form from submitting.. i know return false; will do so i just need to know how i can get this return back to the parent function?

Thanks in advanced!

+2  A: 

I use the following to stop events...

event.returnValue = false;

cresentfresh inspired me to do some research... here is an article with a compatibility matrix.

There are also related threads on SO.

Mayo
caveat: your example is IE only.
Crescent Fresh
You know I've never bothered to check because I've only used it on our intranet. Thanks for the info!
Mayo
Even though return false is supposed to work on every machine, my machine *REQUIRES* that event.returnValue be set to false. I think most machine don't need it, but it's important to know that a tiny fraction do.
GoodEnough
In my reading this morning I saw several threads referencing the combined use of event.returnValue and preventDefault in an effort to catch most browsers.
Mayo
A: 
return validateLogin();

This should do what you want.

Zenham
+2  A: 

You can use it as follow:

return validateLogin();

however, as mmayo pointed out, don't forget about the return value:

event.returnValue = false;
GoodEnough
+1  A: 

You can eventually do that:

return validateLogin();

Note that your function code has some errors (maybe due to the simplification of the code you made to post this question?). You'd better write this method like that:

function validateLogin(){
  ...
  return hi;
}

Note also that insted of having if (hi=true) {, you must write if (hi == true) {, or better if (hi) {...

romaintaz
A: 

Try double == (IF I==5)

Rajasekar
A: 

validateLogin() Function

function validateLogin() {
    return hi;
}

HTML block

<form onsubmit="return validateLogin()">
  ...
</form>
Eli Grey
A: 

The standard way of stoping the default action of an event is:

event. preventDefault();

You may also want to prevent event propagation with event.stopPropgation(); to stop further event listeners from executing.

http://www.w3.org/TR/2001/WD-DOM-Level-3-Events-20010823/events.html#Events-Event

However, IE will not recognize this which is why you can set event.returnValue to false for IE.

eg:

if (event && event.preventDefault) event.preventDefault();
event.returnValue = false;

You can also return false from the event handler for events inlined in HTML.

<form onsubmit="return validateLogin()">

This is not considered best practice however.

Note: the event object is passed in as the first argument in your event listener.

eg:

function validateLogin(e) { 
   e; // is the event object
}

For IE you may need window.event.

function validateLogin(e) { 
   e = e || window.event; // is the event object
}
bucabay