locked
How to check null value using jqueryy RRS feed

  • Question

  • User1839056048 posted

    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

    Tuesday, December 8, 2015 8:23 AM

Answers

  • User1724605321 posted

    Hi klbaiju,

    Code below is for your reference :

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="../Scripts/jquery-1.10.2.js"></script>
        <script type="text/javascript">
            function checking() {
                var containNull = false;
                $('input[type=text]').each(function () {
                    $(this).css("background", "");
                    if ($(this).val().length <= 0) {
                        $(this).css("background", "red");
    
                        containNull = true;
                    }
                })
                if (!containNull) {
                    //call your insert function
                }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Text1" type="text" />
            <input id="Text2" type="text" />
            <input id="Text3" type="text" />
            <input id="Button1" type="button" value="button" onclick="checking()" />
        </div>
        </form>
    </body>
    </html>
    

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2015 9:20 AM