0

My code is this

<script>
 function a(){
  var num = $('#mobile_number).val();
  if !(isNAN(num)){
    alert("Integer")
    return false;
  }
 function b(){
  var addr = $('#addr).val();
  if(addr==""){
   return false;
  }
 }
}
</script>
<form>
 name:<input type="text" id="mobile_number"/>
 Address:<input type="text" id="addr"/>
 <input type="submit" onclick="a();return false;b();return false;"
</form>

but it submit value when i entered wrong input please explain me how can i call and return two function on a click and return if i entered wrong input thanks

6 Answers 6

2

What you need to to return false if either one of a or b returns false.

The solution is to return the logical and value of a() and b().

<input type="submit" onclick="return a() && b()" />
Sign up to request clarification or add additional context in comments.

Comments

1
function validate() {
    return a() && b();
}

onClick="validate();"

Comments

1

Try

onclick="return a();"  

 function a(){
      var num = $('#mobile_number').val();
      if !(isNAN(num)){  
        alert("not a number");        
        return false;
      } 
      var addr = $('#addr').val();
      if(addr==""){
       alert("Address Empty");    
       return false;
      }
    }

Comments

0

Whenever you click on a submit button on a form, it will submit the form always. To call a js function on form submit, use OnSubmit in the form tag like

This way whenever the submit button is clicked, it will first call the two functions, and if things are find, then you can submit the form by using $("#form_id").submit().

Thank you

1 Comment

"Whenever you click on a submit button on a form, it will submit the form always." No.
0

Try this

<form onSubmit="return a(); return b();">
    name:<input type="text" id="mobile_number"/>
    Address:<input type="text" id="addr"/>
    <input type="submit">
</form>

the return false within a and b will stop the form from submitting.

better yet .. make validation one function

<script>
    function validate() {
        var num = $('#mobile_number).val();
        if !(isNAN(num)){
            alert("Integer")
            return false;
        }
        var addr = $('#addr).val();
        if(addr==""){
            return false;
        }
    }
</script>

<form onSubmit="return validate();">

Comments

0

check here

function a(){
  console.log("a");
}


function b(){
  console.log("b");
}


function callBoth(fn1, fn2) {
  fn1(); 
  fn2();

  return result; 
}

callBoth(a, b);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.