• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Problems with onSubmit

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
levyuk
Asked:
levyuk
  • 3
  • 3
1 Solution
 
nabsolCommented:
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
 
nabsolCommented:
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
 
levyukAuthor Commented:
Ok cool ill try those. Is there any reason why my code shouldn't work?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
nabsolCommented:
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
 
levyukAuthor Commented:
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
 
levyukAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now