3

I have a legacy form that I'm trying to update to do simple HTML5 form validation.

Currently, it uses reCaptcha, and invokes a verify() function that validates the reCaptcha challenge before forwarding the contents of the form. I would like to ensure that the form passes HTML5 validation before continuing with the reCaptcha processing, and display the appropriate error messages that the browser would use by default.

The iframe containing the form as the <!DOCTYPE html> doctype. The input fields have the required attribute.

The submit button has the following:

<input type="button" id="script_send" onclick="javascript:verify(this.form);" value="ENVIAR">

The verify script has the basic structure.

function verify(theForm) {
    form = theForm;

    /* Recaptcha processing */
}

I tried using

if (!form.checkValidity()) {
    return false;
}

and even though the form was not submitted, I get no errors displayed on the screen, showing the user what fields they should be providing.

I have seen this jsfiddle [http://jsfiddle.net/5ycZz/] used to demonstrate the checkValidity() function, but even that does not display the visual error cues that I would expect to see, in Chrome, IE10 or FF.

2 Answers 2

3

Try setting the submit event on the form instead of putting a click action on the submit button. The submit event fires AFTER validation so it should only happen if all other constraints passed. This also means you wont have to call form.checkValidity() in your verify function.

<form onsubmit="verify(this)">
...
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

I added the onsubmit for the form, and changed the <input> type of the button to be submit. However, the verify function still seems to get invoked before form validation occurs.
Which browser? i've already confirmed it works in FF. Check fiddle: jsfiddle.net/BqW9D . Maybe you forgot to remove your onclick action?
Actually, I'm a silly bugger. Didn't realise that the (legacy) form redirected to a different URL (which happened to the be the live site, instead of my development environment). This method certainly works!
2

This solving trouble:

if (!form.checkValidity()) {
    form.reportValidity();
    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.