0

I am new to javascript and I am little bit confused about the syntax.

function validateForm()
{
var x=document.forms["myForm"]["fname"].value
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>

My question is If I write onsubmit="return validateForm()" or write onsubmit="validateForm()". What's the difference between these two syntax. So I submitted before just an example.

Here it is example

Event = function(); or Event = return function();

What's the difference between these two syntax

1
  • The difference is that the latter makes no sense and/or is invalid. Do you have any more context for this? Commented May 30, 2011 at 9:06

2 Answers 2

2

They're both invalid.

The return statement should be used within a function. The expression following a return statement is what will be returned from the function, so:

function foo() {
    return 1234;
}

foo(); // => 1234

If you want to assign a function to the Event identifier, then you would do it like so:

Event = function() {
    // function body (what do you want the function to do?)
};
Sign up to request clarification or add additional context in comments.

3 Comments

This is not the exact code. I just ask the difference between these syntax. It is just pseudo code
See my answer for why (posted in a few seconds) I think this is wrong
The OP added a whole bunch of code to the question (a late edit)... rendering my answer seemingly inappropriate.
1

onsubmit="validateForm()" just calls the function and whatever is returned is lost and does not effect anything. But in onsubmit="return validateForm()" the returned value is returned again and if false will not submit the form and if true will continue the submitting process. So you can use it like:

function check()
{
    if(document.getElementByName("fname").value == ""){
        return(false);  //Do not submit the form
    }
    return(true);  //Submit the forum
}

<form name="myForm" action="demo_form.asp" onsubmit="return(check());" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</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.