Hi everyone! I'm really struggling at the moment calling these two functions. The one function checks that all the fields are filled in and the other to validate the date in a certain format. The function that checks all the fields works but when I try to call the second one it stops working.

How can I call both these functions for them to work 100% ? Thanks very much!

Here's the code:

<!DOCTYPE HTML>
<html>
	<head>
                
                <script type="text/javascript">
                    
                    //FIRST FUNCTION

                    function validateForm()
                    {
                    var username=document.forms["myForm"]["username"].value
                    var name=document.forms["myForm"]["name"].value
                    var surname=document.forms["myForm"]["surname"].value
                    
                    //email variables
                    var email=document.forms["myForm"]["email"].value
                    var atpos=email.indexOf("@");
                    var dotpos=email.lastIndexOf(".");
                    
                    //password variables
                    var password=document.forms["myForm"]["password"].value
                    //gender variables
                    var gender=document.forms["myForm"]["email"].value
                    
                    //username validation
                    if (username==null || username=="")
                      {
                      alert("Please enter your username");
                      return false;
                      }
                    
                    //name validation
                    else if (name==null || name=="")
                      {
                      alert("Please enter your name");
                      return false;
                      }
                    
                    //surname validation
                    else if (surname==null || surname=="")
                      {
                      alert("Please enter your surname");
                      return false;
                      }
                    
                    //email validation
                    else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
                      {
                      alert("Please enter a valid email address");
                      return false;
                      }
                    
                    //password validation
                    else if (password==null || password=="")
                      {
                      alert("Please enter your password");
                      return false;
                      }
                      
                    //gender validation
                    else if ( ( document.myForm.gender[0].checked == false ) && ( document.myForm.gender[1].checked == false ) )
                        {
                            alert ( "Please choose your Gender: Male(M) or Female(F)" );
                            return false;
                        }
                    }
                </script>

                //SECOND FUNCTION

                <script type="text/javascript">
                        //date validation
                        function checkdate(input){

                        var validformat = /^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
                        var returnval = false

                        if (!validformat.test(input.value))
                            alert("Please enter the date in this format: dd/mm/yyyy.")
                        else{ 
                            //Detailed check for valid date ranges
                            var dayfield=input.value.split("/")[0]
                            var monthfield=input.value.split("/")[1]
                            var yearfield=input.value.split("/")[2]
                            var dayobj = new Date(yearfield, monthfield-1, dayfield)

                        if ((dayobj.getDate()!=dayfield)||(dayobj.getMonth()+1!=monthfield)||(dayobj.getFullYear()!=yearfield))
                            alert("Invalid Day, Month, or Year range detected.")
                        else
                            returnval = true
                        }
                        if (returnval == false) input.select()
                            return returnval
                        }


                </script>
	</head>
	<body>
              <center>
                  <table cellpadding="1" cellspacing="1">
                      <tr>
                           <td>
                               <center>
                                    <h1>Please enter the following info:</h1>
                                    
                                    <!-- HERE IS THE FUNCTIONS I DON'T KNOW HOW TO CALL -->

                                    <form name="myForm" action="display.php" onsubmit="return checkdate(this.php_date); return validateForm(); " method="post">
                                        
                                            <table border="0" bgcolor="gray" cellpadding="1" cellspacing="1">
                                                    <tr>
                                                            <td>Username: </td><td><input type="text" name="username" placeholder="Enter your username"/></td>
                                                    </tr>
                                                    <tr>
                                                            <td>Name: </td><td><input type="text" name="name" placeholder="Enter your name"/></td>
                                                    </tr>
                                                    <tr>
                                                            <td>Surname: </td><td><input type="text" name="surname" placeholder="Enter your surname"/></td>
                                                    </tr>
                                                    <tr>
                                                            <td>E-mail: </td><td><input type="email" name="email" placeholder="Enter your email address"/></td>
                                                    </tr>
                                                    <tr>
                                                            <td>Password: </td><td><input type="password" name="password" placeholder="Enter your password"/></td>
                                                    </tr>
                                                    <tr>
                                                            <td>Date of birth (dd/mm/yyyy) </td><td><input type="text" name="php_date"/></td>
                                                    </tr>
                                                        <tr>
                                                                <td><input type="radio" name="gender" value="M" />  M  <input type="radio" name="gender" value="F" />  F</td>
                                                        </tr>
                                                    <tr>
                                                            <td><input type="submit" value="Submit" /></td>
                                                    </tr>
                                            </table>
                                    </form>
                                </center>
                            </td>
                      </tr>
                  </table>
              </center>
	</body>
</html>

Joemeister,

Here's a big clue:

<form ..... onsubmit="return checkdate(this.php_date); return validateForm();>

validateForm will not execute regardless of whether checkdate returns true or false.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.