-1

Creating a form,

I have 2 functions

function myValidation() {
   valName();
   idChange();
}
input type="submit" value="submit" onclick="return myValidation();"

But when there are still errors my form submits, but when I put them separately (but not together) in onclick, they work fine and don't submit when there are errors.

alexey
  • 1,381
  • 9
  • 19
josh
  • 123
  • 1
  • 13

2 Answers2

1

You are not using the return values of your valName and idChange functions in your myValidation function and so it will return nothing.

Just adjust your function so it uses them.

function myValidation() {
    return valName() && idChange();
}

That being said, there are sometimes issues with the approach of returning false on an onclick event that are discussed here.

Community
  • 1
  • 1
Marcelo
  • 4,395
  • 1
  • 18
  • 30
  • By any chance are you Marcelo Alves? – Rajshekar Reddy Mar 16 '16 at 13:10
  • Hi marcelo thanks for the reply, I tried the following but it seems like idChange does not work when I do this but valName does work, That being said If I switch the functions then idChange works but valName doesn't idChange() && valName() – josh Mar 16 '16 at 13:11
  • 1
    @Reddy No, I am not. – Marcelo Mar 16 '16 at 13:31
  • @josh Are these functions doing more than just figuring out a return value? In this approach, the functions get called in order until one of them returns false. After that any remaining functions are skipped. If you need to have all the functions be called regardless then you can modify the code to this: http://codepen.io/marceloa/pen/LNxPwX?editors=1010 – Marcelo Mar 16 '16 at 13:38
-1

Try both lines. They will work in all browsers.

event.preventDefault();
event.stopPropagation();
Hatchet
  • 5,320
  • 1
  • 30
  • 42
Aju John
  • 2,214
  • 1
  • 10
  • 27