krzsz
krzsz

Reputation: 37

javascript - Two functions handling a form

I'm in need of help with some javascript handling a form. I'm quite new to this and don't know what to do next. I have two functions with a form: one to check if data is ok and another to merge checkboxes into one record. Is there a way to merge them into one fuction?

Form code:

<form id="formularz_wspolpraca"   name="Zapis na poradnik" method="post" target="_top" onsubmit="return SprawdzFormularz(this)">
<input type="text" id="email" name="email"/>
<input type="text" id="imie" name="imie"/>
<input type="text" id="nazwisko" name="nazwisko"/>
<input type="text" maxlength="12" size="12" id="pole_1" name="pole_1"/>
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3a" value="polecajacy"> 
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3b" value="projektant"> 
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3c" value="instalator"> 
<input class="checkbox_wspolpraca" type="Checkbox" name="pole_3d" value="ekspert"> 
<input type="hidden" name="pole_3" id="pole_3">
<input id="pp" type="checkbox" name="pp" checked=""/>
<input type="submit"  value="Wyślij">
</form>

Check data function:

function SprawdzFormularz(f) {
  if (f.email.value == "") {
    alert("Nie poda\u0142e\u015b/a\u015b adresu e-mail.");
    return false;
  }
  if (((f.email.value.indexOf("@", 1)) == -1) || (f.email.value.indexOf(".", 1)) == -1) {
    alert("Poda\u0142e\u015b/a\u015b b\u0142\u0119dny adres e-mail.");
    return false;
  }
  if (f.imie.value == "") {
    alert("Wype\u0142nij pole Imi\u0119. ");
    return false;
  }
  if (f.nazwisko.value == "") {
    alert("Wype\u0142nij pole Nazwisko. ");
    return false;
  }
  if (f.pole_1.value == "") {
    alert("Wype\u0142nij pole Nr telefonu. ");
    return false;
  }
  if ((f.pole_3a.checked == false) && (f.pole_3b.checked == false) && (f.pole_3c.checked == false) && (f.pole_3d.checked == false)) {
    alert("Wybierz zakres wsp\u00f3\u0142pracy");
    return false;
  }
  if (f.pp.checked == false) {
    alert("Musisz zgodzi\u0107 si\u0119 z Polityk\u0105 Prywatno\u015bci.");
    return false;
  }
  form.submit();
  return true;
}

Merge checkboxes function:

var form = document.getElementById('formularz_wspolpraca');
try {
  form.addEventListener("submit", mergeFuntion, false);
} catch (e) {
  form.attachEvent("onsubmit", mergeFuntion);
}

function mergeFuntion(event) {
  event.preventDefault();
  var boxes = document.getElementsByClassName('checkbox_wspolpraca');
  var checked = [];
  for (var i = 0; boxes[i]; ++i) {
    if (boxes[i].checked) {
      checked.push(boxes[i].value);
    }
  }
  var checkedStr = checked.join(' ');
  document.getElementById('pole_3').value = checkedStr;

  return false;
}

Looking forward to your responses, I'm quite lost.

Upvotes: 0

Views: 66

Answers (1)

Barmar
Barmar

Reputation: 782508

You can simply call both functions in the onsubmit:

onsubmit="return SprawdzFormularz(this) && mergeCheckboxes(this)"

If you want to run the merge function even if the checking function fails, you can set variables:

onsubmit="check = SprawdzFormularz(this); merge = mergeCheckboxes(this); return check && merge"

Upvotes: 1

Related Questions