简体   繁体   中英

Check and alert for the null value

I need to check for the null values and alert them if there are any before getting saved. I have used this code but I am not able to alert the null values instead it is getting saved . .

function fn_publish() {

    var SessionNames = getParameterByName('SessionName');
    var MenuType = getParameterByName('MenuType');
    var Date = getParameterByName('ForDate');
    var publish = "Y";
    Dates = Date.split("-");
    Date = Dates[1] + "/" + Dates[2] + "/" + Dates[0];

    var rows = [];

    cols = document.getElementById('product_table').rows[1].cells.length - 1;
    table = document.getElementById('product_table');

    for (var i = 1; i <= cols; i++) {
        for (var j = 0, row; row = table.rows[j]; j++) {
            if (j == 0) {

                cust = row.cells[i].innerText;
                alert(cust);

            } else if (j == 1) {
                catlg = row.cells[i].innerText;
                alert(catlg);

            } else {

                if (typeof (row.cells[i]) != "undefined") {
                    if (row.cells[i].innerText != "") {
                        //alert(SessionNames+"::"+MenuType+"::"+Date+"::"+catlg+"::"+row.cells[0].innerText+"::"+row.cells[i].innerText+"::"+cust+"::"+publish);
                        fn_insert(SessionNames, MenuType, Date, catlg, row.cells[0].innerText, row.cells[i].innerText, cust, publish);
                    } else {
                        jAlert("Please select a product", "ok");
                        return false;
                    }
                }
            }
        }
    }
    jAlert("Menu Published", "ok");
}
 if (row.cells[i].innerText != "") {

May be the cells are containing empty space in that. Better trim ad then compare.

 if (row.cells[i].innerText.trim() !== "") {

Also instead of innerText use textContent which is common in most modern browsers.

 if (row.cells[i].textContent.trim() !== "") {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM