Techiio-author
Started by Bell GrantNov 6, 2021

Open
Check if inputs are empty using jQuery

2 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWES SHARE

I have a shape that I would love all fields to be stuffed in. If an area is clicked into after which is no longer crammed out, I would like to display a purple historical past.

My code is below:

$('#apply-form input').blur(function () {
  if ($('input:text').is(":empty")) {
    $(this).parents('p').addClass('warning');
  }
});

It applies the warning class regardless of the field being filled in or not.

1 Replies

Techiio-commentatorHarry Lian replied 3 months ago0 likes0 dislikes

Doing it on blur is too limited. It assumes there was focus on the form field, so I prefer to do it on submit, and map through the input. After years of dealing with fancy blur, focus, etc. tricks, keeping things simpler will yield more usability where it counts.

$('#signupform').submit(function() {

var errors = 0;

$("#signupform :input").map(function(){

if( !$(this).val() ) {

$(this).parents('td').addClass('warning');

errors++;

} else if ($(this).val()) {

$(this).parents('td').removeClass('warning');

}

});

if(errors > 0){

$('#errorwarn').text("All fields are required");

return false;

}

// do the ajax..

});

You must be Logged in to reply
Trending Technologies
15
Software40
DevOps47
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance