Closed timothyparez closed 12 years ago
Thanks for bringing this up again. h5Validate was written for AJAX web applications, and the simple form submit use case (while supported) is (intentionally) not default behavior. I need to add this use case to the docs:
$('#form').submit(function () { return $('#form').h5Validate('allValid'); });
Oh, and the Chrome validation is intentionally disabled. h5Validate is an alternative implementation that behaves the same across all browsers (including old IE browsers, mobile browsers, etc...).
It is designed for practical value in eCommerce sites to enable best practice realtime validation based on lots of usability testing. Native browser behaviors don't enable those optimized behaviors, and could potentially damage form usability for the end user.
Oh.. well I only added a form to my page because of the jump start on this page: http://ericleads.com/h5validate/
What I really have is a number of fields on an HTML page and when I click the
What should I use in this case? And can I remove the form (because that one is just causing problems for me anyway)
Update: I just figured out that I needed to add css and can remove the form and use
$(document).ready(function ()
{
$('#settings-form').h5Validate();
});
where #settings-form is simply a div. That works in a sense that it highlights the validation errors. What do I add in my click handler to validate everything before I actually do anything:
$("#btn-save-settings").click(function()
{
/*This is the point where I want to validate on the client side
then I'll send the data to the server where it will be validated again */
});
To test a form for validity:
$('#form').h5Validate('allValid');
This will return true or false and fire an event you can subscribe to. It's perfectly all right to put that in a click handler on any type of element. Test the results and wrap your $.post() in a conditional, so it only gets triggered if the form is valid.
It's probably safe to remove the form, but I like to wrap my elements in a form tag, just for semantics. Just because you have a form, doesn't mean it has to use a traditional submit button.
Ok, sounds good. Does the library also validate
<input type="number" min="1025" max="32768" required/>
Because it returns true even when I enter 99999999
It does not, but it's about time it did.
I'm trying to find h5Validate examples that actually work. Nothing is quite straight-forward enough, so here are my failed attempts:
// Does nothing, not even errorClass
$("form").h5Validate(
{
errorClass: "invalid",
formValidated: function(info){ console.log(info.valid) }
});
// TypeError: 'undefined' is not a function (evaluating '$("form").formValida...
$("form").formValidated(function(info){ console.log(info.valid) });
// TypeError: 'undefined' is not an object (evaluating 'e[b]')
console.log( $("form").h5Validate("allValid") );
// Returns the form, not boolean
console.log( $("form").h5Validate() );
formValidated is not a function that you pass in or call. It's an event that gets triggered on the form element when your form is validated. For the simplest use cases, you don't even need it. See the h5Validate homepage for working example cases.
There aren't any examples; that's my point.
And here's another failed attempt that I'd forgotten to mention:
$("form").on("formValidated", function()
{
console.log(arguments);
});
$("form").h5Validated();
The last example should call $("form").h5Validate()
, not $("form").h5Validated()
(which doesn't exist).
Could you post the full example on jsfiddle.net so I can help you get it working?
Let's save some time and show you a simple working example on jsFiddle.net:
Lots more examples and documentation are available on the h5Validate home page (view source to see how they all work).
Thanks!
"There aren't any examples; that's my point." This is still true to this day, despite dilvie saying " I need to add this use case to the docs:" (above) 11 months ago.
There is no clear example on the homepage for how to check if the entire form is validated. I spent time searching and searching until I found this github issue page where you said "$('#form').submit(function () { return $('#form').h5Validate('allValid'); });" will do the job. This works, but I wish you'd have just have it on the homepage.
The homepage just says "h5Validate supports the following events: and says events such as "formValidated" but does not explain how to use these events. No example code snippets are provided.
Sorry about that. I have a full time job,a book for O'Reilly, and speaking gigs I try to juggle. Interestingly, nobody has ever sent a pull request to add to the docs. Would you like to be the first?
I'm sure he'll make that request. I will too :)
Is there a way to get a list of invalid fields?
Yeah, just listen for the events:
The element in question has been validated. A validity object is passed into the event handler containing:
{
element: HTMLObject, // A reference to the validated element
customError: Bool, // Custom validity failed.
patternMismatch: Bool, // Input does not match pattern
rangeOverflow: Bool, // Input is greater than max attribute value
rangeUnderflow: Bool, // Input is less than min attribute value
stepMismatch: Bool, // Input does not conform to the step attribute setting
tooLong: Bool, // Input is too long
typeMismatch: Bool, // Wrong input type
valid: Bool, // Input is valid
valueMissing: Bool // Required value is missing
}
The form in question has been validated. An object is passed with an object containing a bool, valid, and an array of validity objects corresponding to the validated elements.
Thanks. Is this possible: $("div").h5Validate("allValid");
?
I'm getting these errors even with $("form")
:
TypeError: 'undefined' is not an object (evaluating 'settings[action]') jquery.h5validate.min.js:544
Did you remember to initialize the form?
$(document).ready(function () {
$('form').h5Validate();
});
You have to initialize it before you can "allValid"? Ok.
Is there a way to "destroy"? In mustache/etc apps, this is very important to avoid memory leaks
Initializing it creates the instance settings for you. If you don't initialize it first, it doesn't know what to do.
To destroy, just use jQuery's .remove() method. That will automatically remove event listeners and data references.
I've downloaded the latest release from Github and I'm trying a very simple example, but I can't get it to work: