jQuery Validate ErrorHandling -
i'm using jquery validate plugin. now, if there invalid element element should cleared , selected. furthermore, form must not submitted before validated. code doesn't seem work expected.
what problem ?
here's code:
$("#track").validate({ onsubmit: false, focusinvalid: true, focuscleanup: true, errorelement: "span", errorclass: "invalid", validclass: "valid", highlight: function(element, errorclass, validclass) { $(element).addclass(errorclass); $(element.form).find("span[id=" + element.id + "-error]").addclass(errorclass); ion.sound.play("error"); }, unhighlight: function(element, errorclass, validclass) { $(element).removeclass(errorclass); $(element.form).find("span[id=" + element.id + "-error]").removeclass(errorclass); ion.sound.play("success"); }, rules: { prio: { prio : true, required: true }, lagerort: { place : true, required: true }, ident: { ident : true, required: true } }, messages: { prio: { required: "" }, lagerort: { required: "" }, ident: { required: "" } }, submithandler: function(form) { $(form).ajaxsubmit(); } });
what you're trying unclear, i've identified issues in jsfiddle:
you including version 1.5.2 of plugin many years old. latest version 1.13.1 per the developer's website.
"the form must not submitted before validated"
you've set
onsubmit
false
. per docs, disablesonsubmit
validation; alone allowing submit form without validation. not allowed setonsubmit
optiontrue
, remove it.you've defined custom error messages empty strings, yet you've set
errorelement
optionspan
. makes no sense. if error messages empty strings, not matter kind of container have.return false
inside oferrorplacement
function , block error messages.you're using
highlight
,unhighlight
targetspan
id
ofelement
. since don't have error messages ,span
empty, line totally superfluous.you're using
highlight
set timer triggersfocus
every millisecond. when form invalid, throws infinite loop of blinking elements. removed because plugin automatically bring focus first invalid field.you're using
highlight
set field empty. never work. function triggered on every keystroke if user starts entering valid data, code wipe blank after first character typed.that leaves nothing applying error/valid classes in
highlight/unhighlight
, default behavior already. over-riding options can removed entirely.i activated
submit
button testing purposes.
not sure if meets every requirement, @ least jquery validate plugin working...
http://jsfiddle.net/vke1u6ql/3/
$("#track").validate({ errorclass: "invalid", validclass: "valid", rules: { prio: { prio: true, required: true }, lagerort: { place: true, required: true }, ident: { ident: true, required: true } }, errorplacement: function() { return false; }, submithandler: function (form) { $(form).ajaxsubmit(); } });
Comments
Post a Comment