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!

2
  • 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)..." Commented Aug 28, 2009 at 17:11
  • 1
    Or even better, function validateLogin() { return hi; } Commented Aug 28, 2009 at 17:12

7 Answers 7

6

You can use it as follow:

return validateLogin();

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

event.returnValue = false;
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

4 Comments

caveat: your example is IE only.
You know I've never bothered to check because I've only used it on our intranet. Thanks for the info!
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.
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.
2
return validateLogin();

This should do what you want.

Comments

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
}

Comments

0

Try double == (IF I==5)

Comments

0

validateLogin() Function

function validateLogin() {
    return hi;
}

HTML block

<form onsubmit="return validateLogin()">
  ...
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.