user74843
user74843

Reputation: 701

onsubmit function used for form is coming up undefined

I want to validate my form, and have created a formValidation function with onsubmit, that calls a function from a js.js file. When I click submit, however, I get this error:

enter image description here

Here is the necessary form code:

 <form action="send-email.php" id="callback" class="popup-form" method="post" onsubmit="return validateForm()">
 // Form content
</form>

Here is the function code:

function validateForm() {
  console.log("hellloooooo this is working");
//   var captcha_response = grecaptcha.getResponse();
//   if(captcha_response.length === 0)
//   {
//       // Captcha is not Passed
//       console.log("not working");
//       return false;
//   }
//   else
//   {
//       // Captcha is Passed
//       console.log("working");
//       return true;
//   }
}

The script tag is within the body tag, so it should work.. where am I going wrong??

Upvotes: 0

Views: 2426

Answers (3)

Varsha Rathi
Varsha Rathi

Reputation: 11

Please use input type='submit' in form. It will work in java script as well.

Add submit input in the form as below

<form action="send-email.php" id="callback" class="popup-form" method="post" onsubmit="return validateForm()"><input type="submit">
</form>

And in java script write your validation function as below

<script type="text/javascript">
  function validateForm(){
    console.log('ddd');
    return false;
  }
</script>

Upvotes: 1

Jeson Dias
Jeson Dias

Reputation: 955

This seems to be working. Don't see any issue here

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>

</head>

<body>
    <script>
        function validateForm() {
            console.log("hellloooooo this is working");
        }
    </script>
    <form id="callback" class="popup-form" method="post" onsubmit="return validateForm()">
        // Form content
        <input type="Submit">
    </form>

</body>

</html>

Upvotes: 0

Kasabucki Alexandr
Kasabucki Alexandr

Reputation: 630

You should be sure that your script tag above your form tag

Upvotes: 0

Related Questions