简体   繁体   中英

Use Two Function in Onclick Event with Parameter(JS)

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?

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();" />

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();'/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM