[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problems with onSubmit

Posted on 2006-04-16
6
Medium Priority
?
313 Views
Last Modified: 2008-02-01
Hey guys I have a problem with the onSumbit event of my form which used to work correctly. Heres the code

onSubmit="return validateOnSubmit()"

      function validateOnSubmit(){
            var errors = 0;
            if (!document.forms.addClub.county){
                  return false;
            }else{
                  if (!validateEmail(document.forms.addClub.email, 'inf_email')) errors += 1;
                  if (!isURL(document.forms.addClub.webSite,'inf_website')) errors += 1;
                  if (!validateTelnr(document.forms.addClub.telephoneNo,'inf_phone', true)) errors += 1;
                  if (!validateMenu(document.forms.addClub.county, 'inf_county')) errors += 1;
                  if (!validatePresent(document.forms.addClub.cityTown,'inf_city')) errors += 1;
                  if (!validatePresent(document.forms.addClub.clubAddress,'inf_address')) errors += 1;
                  if (!validateMenu(document.forms.addClub.country, 'inf_country')) errors += 1;
                  if (!validateMenu(document.forms.addClub.activity, 'inf_activity')) errors += 1;
                  if (!validatePresent(document.forms.addClub.motivatingMateID,'inf_mateID')) errors += 1;
                  if (!validatePresent(document.forms.addClub.nameofclub,'inf_name')) errors += 1;
                  if (!validateCheckbox(document.forms.addClub.tandc, 'inf_tandc')) errors += 1;
                  return (errors == 0);
            }
      }

each function within the validateOnSubmit() function works fine for each form element when their onchange event fires so I know the functions work i.e. isURL, validateMenu etc. So I don't understand why if one of the inputs is incorrect the form will still submit. ANyone have any ideaS?
0
Comment
Question by:levyuk
  • 3
  • 3
6 Comments
 
LVL 6

Accepted Solution

by:
nabsol earned 2000 total points
ID: 16465388
Hi

Use this code

function validateOnSubmit(){
          var errors = 0;
          if (!document.forms[0].addClub.county){
               return false;
          }else{
               if (!validateEmail(document.forms[0].addClub.email, 'inf_email')) errors += 1;
               if (!isURL(document.forms[0].addClub.webSite,'inf_website')) errors += 1;
               if (!validateTelnr(document.forms[0].addClub.telephoneNo,'inf_phone', true)) errors += 1;
               if (!validateMenu(document.forms[0].addClub.county, 'inf_county')) errors += 1;
               if (!validatePresent(document.forms[0].addClub.cityTown,'inf_city')) errors += 1;
               if (!validatePresent(document.forms[0].addClub.clubAddress,'inf_address')) errors += 1;
               if (!validateMenu(document.forms[0].addClub.country, 'inf_country')) errors += 1;
               if (!validateMenu(document.forms[0].addClub.activity, 'inf_activity')) errors += 1;
               if (!validatePresent(document.forms[0].addClub.motivatingMateID,'inf_mateID')) errors += 1;
               if (!validatePresent(document.forms[0].addClub.nameofclub,'inf_name')) errors += 1;
               if (!validateCheckbox(document.forms[0].addClub.tandc, 'inf_tandc')) errors += 1;
               if (errors == 0)
               {
                    return true;
               }
               else
              {
                   return false;
              }
          }
     }

Hope this works!

By Nab
0
 
LVL 6

Expert Comment

by:nabsol
ID: 16465403
Hi

Sorry I think addclub is your form name so try this

function validateOnSubmit(){
          var errors = 0;
          if (!document.addClub.county){
               return false;
          }else{
               if (!validateEmail(document.addClub.email, 'inf_email')) errors += 1;
               if (!isURL(document.addClub.webSite,'inf_website')) errors += 1;
               if (!validateTelnr(document.addClub.telephoneNo,'inf_phone', true)) errors += 1;
               if (!validateMenu(document.addClub.county, 'inf_county')) errors += 1;
               if (!validatePresent(document.addClub.cityTown,'inf_city')) errors += 1;
               if (!validatePresent(document.addClub.clubAddress,'inf_address')) errors += 1;
               if (!validateMenu(document.addClub.country, 'inf_country')) errors += 1;
               if (!validateMenu(document.addClub.activity, 'inf_activity')) errors += 1;
               if (!validatePresent(document.addClub.motivatingMateID,'inf_mateID')) errors += 1;
               if (!validatePresent(document.addClub.nameofclub,'inf_name')) errors += 1;
               if (!validateCheckbox(document.addClub.tandc, 'inf_tandc')) errors += 1;
               if (errors == 0)
               {
                    return true;
               }
               else
              {
                   return false;
              }
          }
     }




OR



function validateOnSubmit(){
          var errors = 0;
          if (!document.forms[0].county){
               return false;
          }else{
               if (!validateEmail(document.forms[0].email, 'inf_email')) errors += 1;
               if (!isURL(document.forms[0].webSite,'inf_website')) errors += 1;
               if (!validateTelnr(document.forms[0].telephoneNo,'inf_phone', true)) errors += 1;
               if (!validateMenu(document.forms[0].county, 'inf_county')) errors += 1;
               if (!validatePresent(document.forms[0].cityTown,'inf_city')) errors += 1;
               if (!validatePresent(document.forms[0].clubAddress,'inf_address')) errors += 1;
               if (!validateMenu(document.forms[0].country, 'inf_country')) errors += 1;
               if (!validateMenu(document.forms[0].activity, 'inf_activity')) errors += 1;
               if (!validatePresent(document.forms[0].motivatingMateID,'inf_mateID')) errors += 1;
               if (!validatePresent(document.forms[0].nameofclub,'inf_name')) errors += 1;
               if (!validateCheckbox(document.forms[0].tandc, 'inf_tandc')) errors += 1;
               if (errors == 0)
               {
                    return true;
               }
               else
              {
                   return false;
              }
          }
     }

By Nab


0
 
LVL 7

Author Comment

by:levyuk
ID: 16466010
Ok cool ill try those. Is there any reason why my code shouldn't work?
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.

 
LVL 6

Expert Comment

by:nabsol
ID: 16467138
Hi levyuk

1. Can you post here how you call the function for individual form elements i.e.    onchange="....."

2. Have you written form tag like this

<form name="addClub"  onSubmit="return validateOnSubmit()"> or <form id="addClub"  onSubmit="return validateOnSubmit()">

By Nab
0
 
LVL 7

Author Comment

by:levyuk
ID: 16467638
Form:

<form class="form" onSubmit="return validateOnSubmit()" style="width:100%" method="post" name="addClub" action="processClub.php">

Elements:

<input class="form" type="text" name="nameofclub" id="nameofclub" onChange="validatePresent(this, 'inf_name');">
0
 
LVL 7

Author Comment

by:levyuk
ID: 16502712
I found the problem, was a bloody copy/paste error of mine. Cheers for your help, you can have points for helping me even though the answer isn't there.

Thanks
J
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

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…
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

873 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