Solved

javascript, form submit, onClick

Posted on 2001-07-02
12
646 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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

624 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