Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript, form submit, onClick

Posted on 2001-07-02
12
Medium Priority
?
650 Views
Last Modified: 2012-05-04
Hi,

I have a form in html page. I want to
use javascript to check the fields in the
firm on whether they have been filled when
users click on the submit button. If the fields
are filled out correct, then submit the form;
otherwise don't submit the form.

After reading some javascript tutorial, I know
I should use onClick; but I don't know how.

Could anyone help me out of this?

Thanks a lot.

Jennifer
0
Comment
Question by:jfz139
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 150 total points
ID: 6245757
I would instead use onSubmit in the FORM tag:

<FORM name='myform' action='whatever.asp' onSubmit='return(checkMyForm(this);'>

and in the function checkMyForm, return true or false depending on whether the fields are filled out correctly (true) or not (false)

function checkMyForm(theForm)
{
  // check each field, if it is not filled out correctly, return false


  // all fields are filled out, so ...
  return true;
}
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6245761
oops, left out a parentheses:

onSubmit='return(checkMyForm(this));'

0
 

Author Comment

by:jfz139
ID: 6245852
knightEknight,

Thanks a lot. But it is not working quite well
on my html file. Here is my html page. I expect
to see a popup indication. But it does not do anything.

Do you see anything wrong?

Thanks again.

Jennifer

==============

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

 function checkForm(theForm)
  {

  if(theForm.first_name.length == 0)
   {
    alert("Enter first name")
    document.dataentry.fn.focus()
   }
     
   return false
  }
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="dataentry" action=http://www.abcd.com/form.asp>
 <h2>Form Field Validation</h2>

 Enter First Name:<br>
  <INPUT TYPE="text" NAME="fn" >


 <INPUT TYPE="button"  VALUE="Submit"
     onSubmit='return(checkForm(this));'>

</BODY>
</HTML>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:jfz139
ID: 6245862


Also, after clicking on the submit button, it doesn't
lead me to the asp even if I let the checkForm return
a true.

???

Thanks.

Jennifer
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6245973
points for KnightEKNight

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function checkForm(theForm)
 {

 if(theForm.first_name.length == 0)
  {
   alert("Enter first name")
   theForm.first_name.focus()
   return false;
  }
     
  return true
 }
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="dataentry" action="http://www.abcd.com/form.asp" onSubmit="return checkForm(this)">
<h2>Form Field Validation</h2>

Enter First Name:<br>
 <INPUT TYPE="text" NAME="fn" >


<INPUT TYPE="submit"  VALUE="Submit"
</BODY>
</HTML>
0
 

Expert Comment

by:camasuvi
ID: 6246016
1)  add </form> tag

anyhow i think this is what you are looking for :
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function checkForm(theForm)
 {

 alert("111")
 if(theForm.fn.value.length == 0)
 
  {
   alert("Enter first name")
   theForm.fn.focus()
  }
     
  return false
 }
</SCRIPT>
</HEAD>


<BODY>
<FORM NAME="dataentry" action="http://www.abcd.com/form.asp" onSubmit=" return checkForm(this)" >
<h2>Form Field Validation</h2>

Enter First Name:<br>
 <INPUT TYPE="text" NAME="fn" >
<INPUT TYPE="submit"  VALUE="Submit" >

</form>
</BODY>
</HTML>
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6246022
That too yes :-)
0
 

Author Comment

by:jfz139
ID: 6246072
C J S,

I tried your suggestion and created a page.
But, even if I don't enter anything in the
first name field and clicked the submit, there
is no alert popping up.


I also tried to add the </FORM>, it doesnot
help much neither.

Any more suggestion?

Thanks.

Jennifer

0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6246091
My mistake, change one line and add the /form

if(theForm.first_name.value.length == 0)
0
 

Author Comment

by:jfz139
ID: 6246118
Hi,

All,

I know what was wrong. It was a typo in my
html page. Now it is like the following and it works.

I want to Thank all of you very much.


Jennifer


<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function checkForm(theForm)
{

if(theForm.fn.value.length == 0)
 {
  alert("Enter first name")
  theForm.fn.focus()
  return false;
 }
   
 return true
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="dataentry" action="http://www.abcd.com/form.asp" onSubmit="return checkForm(this)">
<h2>Form Field Validation</h2>

Enter First Name:<br>
<INPUT TYPE="text" NAME="fn" >


<INPUT TYPE="submit"  VALUE="Submit"
</FORM>
</BODY>
</HTML>


0
 

Author Comment

by:jfz139
ID: 6246123
Here is the completed html:

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function checkForm(theForm)
{

if(theForm.fn.value.length == 0)
 {
  alert("Enter first name")
  theForm.fn.focus()
  return false;
 }
   
 return true
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="dataentry" action="http://www.abcd.com/form.asp" onSubmit="return checkForm(this)">
<h2>Form Field Validation</h2>

Enter First Name:<br>
<INPUT TYPE="text" NAME="fn" >


<INPUT TYPE="submit"  VALUE="Submit"
</FORM>
</BODY>
</HTML>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6246243
Sorry all, I was away from my desk and unable to contribute in a timely manner.  Thanks for the points :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question