0
votes

I want to do a very basic validation of a form in html and javascript.

I want a text field to be checked when the submit button is clicked, and the action of the form to be performed if the check is valid, and nothing to happen if not.

For some reason however the onsubmit event of the form does not seem to be triggered at all.

One of the reasons might be that the submit button starts as disabled, but gets enabled by the recaptcha callback function after completion of recaptcha. How would I get around that?

This is my code (I have simplified it to a minimum to test, and nothing happens, the function is not called as the window alert is not popping up:

<script type="text/javascript">
   function capcb() {
       document.getElementById("cap_sub").disabled = false;
    };                                 
</script>
<script type="text/javascript">
    function check() {
        window.alert("checking");
        return false;
    }
</script>
[...]
<form action="submitpage.php" method="post" onsubmit="return check()">
  <fieldset>
    <input type="text" name="Email" id="email" maxlength="120" class="input" /></p> 
  </fieldset>
  <div align="center" class="g-recaptcha" data-sitekey="masked" data-size="compact" data-callback="capcb"></div>
  <input type="submit" id="cap_sub" value="Submit!" class="submit"/>
</form>

Loooking forward to your suggestions !

2
I am not sure what you mean since it gets triggered for me, see this fiddle. Could you post your complete markup or create a fiddle, please? - Anonymous
Yes, it wroks for me too! jsfiddle.net/qkkvcsa7 - MountainConqueror

2 Answers

0
votes

Maybe because you have a semicolon at the end of the function capcb()? Because i tried your code and its working.

<script type="text/javascript">
   function capcb() {
       document.getElementById("cap_sub").disabled = false;
    }                                
</script>
0
votes

I am so sorry. I literally spent 3 hours before deciding to ask here. And I finally realized I had a stupid syntax error in my check() function - the complete one that is, not what I posted, which rendered the call() function undefined.. Fixed it and it works... I'm so used to compiling code before executing it, having inherited this old website to maintain as a volunteer is a nightmare for the cobol coder that I am :)

I really apologize if I wasted anyone's time!