2

Confusing title, I know. I'll try to word this as best as I can.


What I have

A large form made up of multiple sections. Only one section is visible at one time, and clicking the Continue button just displays the next part of the form using javascript. Some inputs contain the type='email' attribute.

enter image description here


What I want to happen

When the user clicks a Continue button, I want the browser to think the form is being submitted so it does its checks on the type='email' inputs and displays a message if the input is not valid, just like this. enter image description here

But I do not want it to submit the form when the Continue button is clicked.

EDIT: Sorry, I forgot to add that I also need the form to submit at the end. So I cannot use the onsubmit='return false' approach.


What currently happens

At the moment, the browser validation activates when I click the final Submit button (because the form is actually being submitted). But this is too late because the form sections are all closed now, and users will not be able to see which input needs attention.

Dan Johnson
  • 1,480
  • 17
  • 36
  • Title is... Well, it is somewhat strange. – nicael Jun 11 '14 at 16:25
  • 5
    This might help http://stackoverflow.com/questions/7548612/triggering-html5-form-validation – flec Jun 11 '14 at 16:27
  • 2
    trigger your validation `onblur` of `input` – Sharky Jun 11 '14 at 16:27
  • If you are going to use `javascript` on your page, you might as well use `jquery` in conjunction with the `jquery validation plugin`. It will take more work than simply using html attributes to enforce validation but it will take much less work than writing the `javascript` to validate the page yourself. Plus, it gives you a lot more control of when validation occurs and what happens when validation occurs. – elitechief21 Jun 11 '14 at 16:34
  • 1
    why don't you just have your continue button check validity of inputs and then progress to next page. – theonlygusti Jun 11 '14 at 16:34
  • do you want to perform client side validation or server side validation? if server side then submit your form as a target to an iframe – ɹɐqʞɐ zoɹǝɟ Jun 11 '14 at 16:38
  • also related: http://stackoverflow.com/questions/7002230/trigger-standard-html5-validation-form-without-using-submit-button – fsw Jun 11 '14 at 16:39
  • @elitechief21 I'm using javascript for validation anyway, but I still want to keep the html5 attribute for the sake of semantic code and also so the keyboard layout changes on some devices. – Dan Johnson Jun 12 '14 at 07:40

3 Answers3

2

You need add form and submit button attributes:

<form onSubmit="return false;">
    <input type="email" /> <!-- this will validate! -->
    <button type="submit" onClick="showNextForm();">Continue</button>
</form>

No code was shown by you, so I can't optimize yours appropriately, but this is essentially what you need to do to your <form> and <button>.

The form will validate, but will not submit!

StaticVoid
  • 1,539
  • 10
  • 11
0

Here is an approach you can use.

$(function(){
    $('#continue').click(function(e){
        if(!$('#email')[0].checkValidity()){
            $('#form').find(':submit').click();
        }
        else {
            //Open next section
            alert('Opening next section');
        }
    });
});

Working jsfiddle http://jsfiddle.net/mrodriguezr/5bzNx/3/

Melvinr
  • 514
  • 2
  • 8
-2

Try

<form onsubmit="return yourJSCheckFunction();">
user2810895
  • 1,284
  • 3
  • 19
  • 33