I have a form that I would like all fields to be filled in. If a field is clicked into and then not filled out, I would like to display a red background.
Here is my
You can try something like this:
$('#apply-form input[value!=""]').blur(function() {
$(this).parents('p').addClass('warning');
});
It will apply .blur()
event only to the inputs with empty values.
With HTML 5 we can use a new feature "required" the just add it to the tag which you want to be required like:
<input type='text' required>
if ($('input:text').val().length == 0) {
$(this).parents('p').addClass('warning');
}
Here is an example using keyup for the selected input. It uses a trim as well to make sure that a sequence of just white space characters doesn't trigger a truthy response. This is an example that can be used to begin a search box or something related to that type of functionality.
YourObjNameSpace.yourJqueryInputElement.keyup(function (e){
if($.trim($(this).val())){
// trimmed value is truthy meaning real characters are entered
}else{
// trimmed value is falsey meaning empty input excluding just whitespace characters
}
}
A clean CSS-only solution this would be:
input[type="radio"]:read-only {
pointer-events: none;
}
you can use also..
$('#apply-form input').blur(function()
{
if( $(this).val() == '' ) {
$(this).parents('p').addClass('warning');
}
});
if you have doubt about spaces,then try..
$('#apply-form input').blur(function()
{
if( $(this).val().trim() == '' ) {
$(this).parents('p').addClass('warning');
}
});