Created
August 26, 2014 08:05
-
-
Save RemeJuan/c90218ec2e81b33fdabc to your computer and use it in GitHub Desktop.
Very basic form validation for jQeury, only useful if you do not want to use wbshims to pollyfill older browsers and simply nee to validate if a field has a value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Basic custom form validation | |
$('#formSave').on('click', function() { | |
$('input[data-required="true"]').each(function(){ | |
if($(this).val() === '') { | |
$(this).addClass('required').val('This field is required'); | |
} | |
}); | |
if (cfarray.length === 0) { | |
$('#selectedResults').addClass('required').text('This field is required') | |
} | |
$('select[data-required="true"]').each(function(){ | |
if($(this).val() === '') { | |
$(this).addClass('required').append('<option class="required" selected>This field is required</option>'); | |
} | |
}); | |
if( $('.required').length === 0 ) { | |
valid = true; | |
} | |
if (valid === true) { | |
$('form').trigger('submit'); | |
} else { | |
return false; | |
} | |
}); | |
//Clear form validation properties | |
$('input').on('focus', function(){ | |
if($(this).val() == 'This field is required') { | |
$(this).removeClass('required').val(''); | |
} | |
}); | |
$('#addCustomer').on('focus', function(){ | |
if (cfarray.length === 0) { | |
$('#selectedResults').removeClass('required').text(''); | |
} | |
}); | |
$('select').on('click', function(){ | |
$(this).removeClass('required'); | |
}); | |
$('select').on('change', function(){ | |
$(this).find('option.required').remove(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Basic custom form validation | |
$('#formSave').on('click', function() { | |
$('[data-required="true"]').each(function(){ | |
if($(this).val() == '') { | |
$(this).addClass('required'); | |
$(this).parent('td').addClass('required'); | |
} | |
}); | |
if( $('.required').length == 0 ) { | |
valid = true; | |
} | |
if (valid == true) { | |
$('form').trigger('submit'); | |
} else { | |
return false; | |
} | |
}); | |
//Clear form validation properties | |
$('input').on('focus', function(){ | |
if($(this).hasClass('required')) { | |
$(this).removeClass('required'); | |
$(this).parent('td').removeClass('required'); | |
} | |
}); | |
$('#addCustomer').on('focus', function(){ | |
if (cfarray.length == 0) { | |
$('#selectedResults').removeClass('required'); | |
} | |
}); | |
$('select').on('click', function(){ | |
$(this).removeClass('required'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment