Calling a function within a function problem

Discussion in 'JavaScript' started by j_o, Jul 13, 2011.

  1. #1
    Hi everyone,

    I am trying to create some form validation. I have created a few functions that check different fields using the onchange property and if they fail they give a small alert message. Now I want to put those small functions into one function. I then want to use onsubmit to check once again on submission and make sure the the user actually heeded the error and fixed the problem. If they didnt the function will return false and not submit.

    Here is the top part of my form:

    <form method="post" action="user_cp.php?action=<?php echo(md5("ProcessInfoUpdate")); ?>" id="UpdateInfoForm" name="UpdateInfoForm" onsubmit="javascript:return FullValidationUpdateInfo();">
    HTML:
    The javascript code is:

    
    function validate(form_id,email) {
     
       var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
       var address = document.forms[form_id].elements[email].value;
       if(reg.test(address) == false) {
     
          alert('Invalid Email Address');
          return false;
       }
    }
    
    function CheckHomePhone()
    {
       if(document.UpdateInfoForm.HomePhone.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
       {
          alert("The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.");
          return false;
       }
    }
    
    function CheckAlternatePhone()
    {
       if(document.UpdateInfoForm.AlternatePhone.value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
       {
          alert("The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.");
          return false;
       }
    }
    
    function FullValidationUpdateInfo(){
    	return CheckHomePhone();
    	return CheckAlternatePhone();
    	return validate('UpdateInfoForm','Email');
    }
    
    Code (markup):
    I dont really know much javascript and I am just kinda learning as I throw stuff together so hopefully I am not making a major mistake and it is easy to fix. Thanks in advance for the help!

    Also the functions that are being called all work with the onchange property in my form.
     
    j_o, Jul 13, 2011 IP
  2. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #2
    
    function FullValidationUpdateInfo(){
    	return CheckHomePhone();
    	return CheckAlternatePhone();
    	return validate('UpdateInfoForm','Email');
    }
    
    Code (markup):
    Change this function to:

    
    function FullValidationUpdateInfo(){
      if (CheckHomePhone() == false) return false;
      if (CheckAlternatePhone() == false) return false;
      if (validate('UpdateInfoForm','Email') == false) return false;
      return true;
    }
    
    Code (markup):
     
    dthoai, Jul 22, 2011 IP