JQUERY VALIDATE - ONKEYUP - OPTIONAL
by bizamajig
HTML
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.16.0/jquery.validate.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.16.0/additional-methods.js"></script>
<form id="myform">
<input type="text" name="enterprise_name" data-rule-required="true" data-rule-onkeyup="start" data-rule-minlength="6" />
<br/>
<input type="submit" />
</form>
<a id="docs" href="http://docs.jquery.com/Plugins/Validation" target="_blank">Validation Documentation</a>
CSS
#docs {
display: block;
position: fixed;
bottom: 0;
right: 0;
}
JavaScript
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
onkeyup: function( element ) { // turn off onkeyup validation for remote url/ajax checking fields
var input__id = $( element ).prop( 'id' ) || $( element ).prop( 'name' ),
input__onkeyup = $( element ).data( 'rule-onkeyup' ) || 'event';
input__id = input__id || '';
//DEBUG VALIDATE//console.log( 'DEBUG - VALIDATE - ONKEYUP', input__id, this.settings.rules[ input__id ].onkeyup );
if (
( input__onkeyup === 'event' )
) {
/*DEBUG VALIDATE*/console.log( 'DEBUG - VALIDATE - ONKEYUP - EVENT');
if ( event.which === 9 && this.elementValue( element ) === '' ) {
return;
}
else {
if ( element.name in this.submitted || element === this.lastElement ) {
this.element( element );
}
}
}
if (
( input__onkeyup === 'start' )
) {
/*DEBUG VALIDATE*/console.log( 'DEBUG - VALIDATE - ONKEYUP - START');
if ( event.which === 9 && this.elementValue( element ) === '' ) {
return;
}
else {
this.element(element);
}
}
// $.validator.defaults.onkeyup.apply( this, arguments );
/*DEBUG VALIDATE*/console.log( 'DEBUG - VALIDATE - ONKEYUP - RETURNING');
},
/*
// START IMMEDIATELY
onkeyup: function (element, event) {
if (event.which === 9 && this.elementValue(element) === "") {
return;
} else {
this.element(element);
}
},
*/
/*
// START AFTER FIRST VALIDATION EVENT, LIKE SUBMIT
onkeyup: function( element, event ) {
if ( event.which === 9 && this.elementValue(element) === "" ) {
return;
} else if ( element.name in this.submitted || element ===...