Merging onsubmit scripts

I have a form which has two scripts that run on submit; one to disable the submit button after being clicked once, the other to add the uk country code to the phoneNumber field. I can only get one of them to run at a time, how can i get them to both run?

  <form action="https://123.com" method="post" onsubmit="return checkForm(this);"> 
   <input name="PhoneNumber" type="tel" required="">
   <input name="myButton" type="submit" value="Submit">
   </form>
 <script type="text/javascript">
  function checkForm(form)
  {
    //
    // validate form fields
    //

    form.myButton.disabled = true;
    return true;
  }
</script> 
  <script>
function checkForm(f) {
  const phoneNumberField = f.querySelector("[name='PhoneNumber']");
  if(phoneNumberField.value.startsWith("0")){
    phoneNumberField.value = "44" + phoneNumberField.value.substring(1);
  }
   return true; // Just for demo to stop the form submitting
}
  </script> 
 

Leave a Comment