How to check null value in jquery [Resolved]

Posted by Klbaiju under jQuery on 12/8/2015 | Points: 10 | Views : 896 | Status : [Member] | Replies : 2
Hi, i want to check any null value in textbox using jquery.

in my application, want to check nearly 20 text boxes.

this is my code. a function for checking null value

function checking() {
var empty = 0;
$('input[type=text]').each(function () {
if (this.value == "") {
empty++;

return false;
}
})

}


following is code for inserting

 $('#ainsert').click(function (e) {//a1insert
e.preventDefault();
);

var vtype = $('#txtvtype').val();
var vloc = $('#txtlocation').val();
var vchrid = $('#txtvoucherid').val();
var vdate = $('#txtvoucherdate').val();
var vamt = $('#txtcash').val();

$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "CashPaymentVoucher2.aspx/InsertVoucher",

data: "{Vtype:'" + vtype + "',Vloc:'" + vloc + "',Vdate:'" + vdate + "',Vaccount:'" + vamt + "'}",
dataType: "json",
success: function (data) {
alert(data.d);
},
error: function (xhr) {
alert(xhr.responseText);
}
});

});

My requirement is if any textbox is null .insert button should'nt fired and empty text box's background clor should be red.

How it is possible.

Regards

Baiju




Responses

Posted by: Rajnilari2015 on: 12/8/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
1
Down

Resolved
Try this

<html>

<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js'></script>
<script type="text/javascript">
var flag=true;
$(document).ready(function () {

$("#btnSubmit").click(function () {

$(".txtClass").each(function() {

var txtValue = $(this).val();

if (txtValue.length <= 0) {
$(this).css({"background-color": "red"});
flag =false;
} else{
$(this).css({"background-color": "white"});
}
});

if(flag) {
alert('do something interesting');
}
});
});

</script>
</head>
<body>

<input type="text" id="txt1" class='txtClass'/>
<input type="text" id="txt2" class='txtClass'/>
<input type="text" id="txt3" class='txtClass'/>
<input type="text" id="txt4" class='txtClass'/>
<input type="text" id="txt5" class='txtClass'/>
<input type="button" id="btnSubmit" value="Insert"/>

</body>
</html>


Hope this helps.

--
Thanks & Regards,
RNA Team

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Professionaluser on: 12/8/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down

Login to post response