Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if value is null using jQuery [duplicate]

Tags:

jquery

I'm trying to check if the value of my variable is null but it does not work. And i also have to stop script and don't insert the row

Html

<input type="text" name="for_intitule" id="form_intitule">

Jquery

var form_intitule = $('input[name=form_intitule]').val();

if(form_intitule == null){
  alert('fill the blank');
  return false;
}

UPDATE :

$('#insertForm').on('click', function(){
  var form_intitule = $('input[name=form_intitule]').val();

  if($('input[name=form_intitule]').val().trim().length == 0){
    alert('fill the blank');
  }
  $.ajax({
    type: "GET",
    url: "lib/function.php?insertForm="+insertForm+"&form_intitule="+form_intitule,
    dataType : "html",
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
    },
    success:function(data){
    }
  });
});
like image 758
Lucas Frugier Avatar asked Dec 01 '25 20:12

Lucas Frugier


1 Answers

The .val() always returns a string. You can check if the string is empty using:

.val().trim().length == 0

So your full condition will be:

var form_intitule = $('input[name=form_intitule]').val().trim();

if (form_intitule.length == 0) {
  alert('fill the blank');
  return false;
}
like image 78
Praveen Kumar Purushothaman Avatar answered Dec 11 '25 08:12

Praveen Kumar Purushothaman