Solved

javascript, form submit, onClick

Posted on 2001-07-02
12
641 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 50 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 72
Need to click button twice to get results 2 34
jquery conditionally populate list boxes 2 25
how can i remove string values 2 17
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now