2

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 advance!

Juergen
  • 12,378
  • 7
  • 39
  • 55
tarnfeld
  • 25,992
  • 41
  • 111
  • 146
  • 1
    Just a note; where it says "if (hi=true)...", this is not checking for equality, it is setting the variable. Should be "if (hi==true)...", or better yet, "if (hi)..." – Funka Aug 28 '09 at 17:11
  • 1
    Or even better, function validateLogin() { return hi; } – Funka Aug 28 '09 at 17:12

7 Answers7

6

You can use it as follow:

return validateLogin();

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

event.returnValue = false;
mbillard
  • 38,386
  • 18
  • 74
  • 98
3

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.

Community
  • 1
  • 1
Mayo
  • 10,544
  • 6
  • 45
  • 90
  • 1
    caveat: your example is IE only. – Crescent Fresh Aug 28 '09 at 15:43
  • You know I've never bothered to check because I've only used it on our intranet. Thanks for the info! – Mayo Aug 28 '09 at 15:43
  • 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. – mbillard Aug 28 '09 at 18:35
  • 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 Aug 28 '09 at 19:13
3

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) {...

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
2
return validateLogin();

This should do what you want.

markh
  • 783
  • 4
  • 10
2

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
  • 5,235
  • 2
  • 26
  • 37
0

Try double == (IF I==5)

Rajasekar
  • 18,392
  • 34
  • 106
  • 137
0

validateLogin() Function

function validateLogin() {
    return hi;
}

HTML block

<form onsubmit="return validateLogin()">
  ...
</form>
Eli Grey
  • 35,104
  • 14
  • 75
  • 93