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){
}
});
});
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With