Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if input field is empty

Tags:

jquery

I'm making a form with inputs, if the input type is empty then the button submit is disabled but, if the input fields is with length > 0 the submit button is enabled

<input type='text' id='spa' onkeyup='check()'><br>
<input type='text' id='eng' onkeyup='check()'><br>
<input type='button' id='submit' disabled>

function check() {
  if($("#spa").lenght>0 && $("#eng").lenght>0) {
    $("#submit").prop('disabled', false);
  } else {
    $("#submit").prop('disabled', true);
  }
}

it works but if then I delete for example the content of input spa the button submit is still enabled

like image 709
Kakitori Avatar asked Mar 07 '13 10:03

Kakitori


2 Answers

Use trim and val.

var value=$.trim($("#spa").val());

if(value.length>0)
{
 //do some stuffs. 
}

val() : return the value of the input.

trim(): will trim the white spaces.

like image 63
Ravi Gadag Avatar answered Sep 27 '22 00:09

Ravi Gadag


use .val(), it will return the value of the <input>

$("#spa").val().length > 0

And you had a typo, length not lenght.

like image 41
gdoron is supporting Monica Avatar answered Sep 24 '22 00:09

gdoron is supporting Monica