0

When I submit a web form I call two functions, like this:

<form action="myaction" name="myform" method="post" onsubmit="return submithandler(this) && validate(this)">

The javascript:

function submithandler (form) {
// a function that replaces some diacritical marks to the correct form
return true;
};

function validate(form) { 
// huge validation code
};

Works fine in all browsers, except Firefox; this browser does the submithandler(this) part, but ignores the validate(this). If I make the form tag like this (below), it does the validation but ignores submithandler(this).

<form action="myaction" name="myform" method="post" onsubmit="return validate(this) && submithandler(this)">

Any ideas please?

EDIT:

The Firefox problem must be within this script? Maybe var form = event.target; ? Please see here: Change characters on form submit

// The script replaces all instances of a letter (or whatever) inside all text fields in the form.

 function submithandler (form) {
 var form = event.target;
 var i, l;
  for (i = 0, l = form.elements.length; i < l; i += 1) {
 if (form.elements[i].type === 'text') {
  form.elements[i].value = form.elements[i].value.replace(/Ş/g, 'Ș');
  form.elements[i].value = form.elements[i].value.replace(/ş/g, 'ș');
  form.elements[i].value = form.elements[i].value.replace(/Ţ/g, 'Ț');
  form.elements[i].value = form.elements[i].value.replace(/ţ/g, 'ț');
    }
    }
     return true; 
     };
Community
  • 1
  • 1
Malasorte
  • 1,153
  • 7
  • 21
  • 45
  • Have you tried `onsubmit="return(validate(this) && submithandler(this))"` ? Note that your approach may be wrong in the first place. Maybe you should just launch the submit handler and call the validation method from inside it. And it would also be better not to use inline javascript. – Laurent S. Apr 11 '14 at 08:38
  • Is `validate()` returning true? – Deanna Apr 11 '14 at 08:43
  • onsubmit="return(validate(this) && submithandler(this))" does not do the trick. – Malasorte Apr 11 '14 at 08:45
  • @ Deanna - validate() is composed of many "ifs". They all return false. – Malasorte Apr 11 '14 at 08:46
  • If it never returns true, then `submithandler` will never be called. This short circuiting behaviour of `&&` is common across all similar languages. – Deanna Apr 11 '14 at 09:02
  • But it works in all browser, except Firefox... – Malasorte Apr 11 '14 at 09:39

1 Answers1

2

call the validate function inside the submithandler function:

function submithandler (form) {
// a function that replaces some diacritical marks to the correct form
if(isValid(form)){
   return true;
} else{
   return false;
}
};



    function isValid(form) { 
    // huge validation code
    //validation code: must return true if valid
       if(valid){ 
    return true;
    } else {
    return false;
    }
    };
mel3kings
  • 8,857
  • 3
  • 60
  • 68
  • How can I do that please? Javascript level: beginner. – Malasorte Apr 11 '14 at 08:42
  • When I add validate(form) inside submithandler (form), the form is submitted right after the first validation alert ("please enter something"). The validation is called but the form is submitted without all the inputs field being checked... – Malasorte Apr 11 '14 at 08:52
  • Even if I remove return true; from submithandler (form). – Malasorte Apr 11 '14 at 08:54
  • if this function is mainly used for validation i suggest you use jquery validation. http://stackoverflow.com/questions/15060292/a-simple-jquery-form-validation-script – mel3kings Apr 11 '14 at 08:55
  • @melt321 If validate is supposed to detect errors, then your sample is ignoring the result. – Deanna Apr 11 '14 at 09:03
  • @ melt321. Thank you, used your suggestion. This works, just like my original script. But Firefox still ignores the validation part. I will edit my original post, and include the submithandler script. The problem must be from within this script. – Malasorte Apr 11 '14 at 09:35
  • @ melt321 Partailly resolved this by calling scripthandler inside validate. Now Firefox returns validation and ignores the submithandler, small price I can pay. Thank you again!! – Malasorte Apr 11 '14 at 10:03