Link to home
Start Free TrialLog in
Avatar of levyuk
levyuk

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of nabsol
nabsol
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


Avatar of levyuk
levyuk

ASKER

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

ASKER

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');">
Avatar of levyuk

ASKER

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