1

I'm currently developing a form which will be powered by HTML5 features and jQuery. One of the things I've been tasked with is ensuring that the native "Please fill in this field" message is still available to browsers which natively support validation.

My setup looks like so:

|----------|
|   FORM   |
|----------|
===BUTTON===

The form has several parts to it, and so the button is global across them all. The form then slides to the next section if complete.

Here is what I have now, this correctly fires the button event to the form and triggers a submit event.

$(".next").click(function() {
    var $incoming = $(".nextPart #" + (currentSlide));
    var incomingID = $incoming.data("cardid");
    var partCode = "form-" + incomingID;

    $("form[name='" + partCode + "']").trigger("submit");
});

$("form").bind('submit', function(event) {
    var goForth = true;

    if(!event.currentTarget.checkValidity()) goForth = false;

    if(!goForth) return false;

    /* Do some stuff with progress bar and more things */

    return true;
});

However, even though the form submit fails, there is no validation message. Is there a way to pragmatically fire this on an element, or have I done something stupid?

For clarification, this is a screenshot of the validation message I am on about. enter image description here

5
  • event.currentTarget is a DOM node -- have you given it the checkValidity property ? Commented Jul 19, 2011 at 12:43
  • No, but when I used console.log() it displayed that as a property. Commented Jul 19, 2011 at 12:45
  • is that property a function -- typeof event.currentTarget == 'function' Commented Jul 19, 2011 at 12:46
  • false and console.log(typeof event.currentTarget); reports an object Commented Jul 19, 2011 at 12:48
  • 1
    +1 for having a variable reference called goForth Commented Jul 19, 2011 at 15:23

2 Answers 2

1

instead of event.currentTarget.checkValidity -- do this...

function checkValidity(elem) {
 // check validity here
 // retun true/false
}

and then in your submit handler do this...

if(checkValidity(event.currentTarget)) { ... 

Also, it is generally NOT a good idea to trigger native browser events -- trigger is good for custom events -- if you need to submit the form you can call the submit() method of the form object like this..

$("form[name='" + partCode + "']").get(0).submit();
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't really help me in showing the validation message, I'm capable of manually validating myself, but since there is no way of doing what I want, in the format I have, I'll accept your answer.
I've actually unaccepted your answer because the way I fixed this wasn't really linked to what you suggested.
0

As I described in my first post, I was using a click event on a button which was in a global scope. This was then sending a submit action to the form which, although was sending the form to be submitted, it wasn't firing the bubble event (whatever the hell that is) on the elements.

To fix this, I wrapped all of my slides in one form instead of multiples. I kept my submit button outside of the slides, so it still acted as a global navigation item.

Removing the click event from this button and changing the type of it to submit now gets the bubble displaying.

Not the best fix, since the bubble should be able to be trigged without having to submit the form, however, I guess with HTML5 validation, you can define the parameters for what is accepted and what isn't.

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.