0

This is my Two Functions with parameters:

<script language="JavaScript" type="text/javascript">
 function code(input){ 
  if(input.match(/^[0-9]{11}$/)){
  document.getElementById('ErrMsg').innerHTML += "Phone Is Valid"
    return true;
  }
   document.getElementById('ErrMsg').innerHTML += "Enter Valid Phone Number"
   return false;
 }
  function codee(input){ 
  if(input.match(/^[a-z]{11}$/)){
  document.getElementById('ErrMsg').innerHTML += "Name is Valid"
    return true;
  }
   //alert("Enter a Valid Phone Number");
   document.getElementById('ErrMsg').innerHTML += "Enter Your Valid Name"
   return false;
   }
  </script>

my JS code that is below:

 <input type=text size=12 name=lname id=lname/>
 <input type=text size=12 name=Phone id=Phone/>
 <input type=submit border=0 value=OK  onclick='return code(document.getElementById("Phone").value)' />

how can i use two function in onclick of form?

3
  • "this problem" What problem? There is no description of a problem in your question. Commented Dec 5, 2014 at 15:06
  • toh problem kya hai ??(what is the problem ?) Commented Dec 5, 2014 at 15:08
  • the problem is obvious how can i use two function with parameter in onclick of form? Commented Dec 5, 2014 at 15:44

2 Answers 2

2

You could call a single new method that collects the form inputs and checks them one at a time:

function validate() {
    var isValid = true;

    isValid = isValid && code(document.getElementById('Phone').value));
    isValid = isValid && codee(document.getElementById('lname').value));

    return isValid;
}

Your button changes to:

<input ... onclick="return validate();" />
Sign up to request clarification or add additional context in comments.

4 Comments

More info on bitwise AND assginment operator here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@PoelincaDorin: Thanks for the link.. after you posted it though I decided I didn't like its usage here.
I didn't liked it either, however i always find it useful to post these kinds of links in the answer as it gives new programmers an entry point to the docs :)
yeah thanks, with a bit changes it`s correctly usable.
1

You could do something like this:

<script language="JavaScript" type="text/javascript">
 function callBoth(input){ 
     return code(input) && codee(input);
 }

  </script>

Whether you AND or OR is up to your logic.

In your button:

 <input type=submit border=0 value=OK  onclick='return callBoth(document.getElementById("Phone").value)'/>

Based on your comment, you need to change your code like so:

<script language="JavaScript" type="text/javascript">
 function callBoth(){ 
     return code(document.getElementById("Phone")) && codee(document.getElementById("lname"));
 }

  </script>

and your button

 <input type=submit border=0 value=OK  onclick='return callBoth();'/>

3 Comments

and what about lname? how to do it? like this: onclick='return code(document.getElementById("Phone").value);''return code(document.getElementById("lname").value);'
I'm not sure what you are referring to, as it does not feature in your question. If this is for form validation, I would recommend looking at a plugin such as jquery validate, as this will make this a lot simpler for you.
yeah this is form validation. but in your code callBoth(document.getElementById("Phone").value) this is just doing for "phone", what about for "lname"? this is problem?

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.