-
at this time, your boss want the forms will be validated one by one.
//"short" means the rest of input will not be validated once a input is fail
//"short" is invalid in data-rule
new SMValidator('form', {
short: true
});
numbers:
email :
-
but for the single input, your boss want it can validate all rules and show him all the failure message.
//replace "|" with "&", it can validate all rule of input
//the failure message is splited with "<br>"
//required is special, it is not together with rules
//"&" and "|" can't appear at the same time
new SMValidator('form', {
fields: {
test6: {
required: true,
rule: 'number&length(2,)',
failHtml: '<div style="color:#f00"></div>'
}
}
});
<!--data-rule format-->
<input type="text" data-rule="required&number&length(2,)&failHtml(<div style='color:#f00'></div>)">
-
of course not enough, your boss need the messages in a row and split with ';' and contained in '[]'.
<input type="text" name="test7">
<span id="failMsg" style="color:#f00"></span>
<script>
//"fail" is a callback function when validate fail, and pass back the message. the message is string or array, according to "|" or "&"
//"pass" is a callback function when validate pass
//input is the context of "fail" and "pass", so "this" is input in the callback
new SMValidator('form', {
fields: {
test7: {
required: true,
failHtml: false, //disbale failHtml
rule: 'number&length(2,)',
fail: function(messages) {
document.getElementById('failMsg').innerText = '[' + messages.toString().replace(',', ';') + ']';
},
pass: function() {
document.getElementById('failMsg').innerText = '';
}
}
}
});
</script>
-
at the end of this tutor, What didn't mention about SMValidator?
there are other features that supporting bootstrap/semantic ui and validating checkbox/radio/select/textarea.
you can see the demo for detail. if you don't need the default style and rule, you can use the pure file.
(end)