HEEN
HEEN

Reputation: 4721

Amount should not be less than 0 in grid using javascript not working

When I go to Insert first record into the gridview. What I want is, My Amount column should not be 0 or less than that.

Here is what my code looks like.

function checkAgrmVal() {
        for (var i = 0; i < GridPayInfo.Rows.length; i++) {
            var AgrmntAmt = GridPayInfo.Rows[i].Cells[5].Value;
            alert(AgrmntAmt);
            if (AgrmntAmt <= "0") {
                alert('Agreement amount cannot be 0 or less than 0');
                return false;
            }
        }
    }

It doesn't works when I go to add the first row, but it does work when I go to add the second row.

Upvotes: 0

Views: 72

Answers (1)

Mamdouh Saeed
Mamdouh Saeed

Reputation: 2324

  • To get table rows use GridPayInfo.rows[i].cells[5] not Rows and Cells
  • To get Cell content use innerText, textContent to get text or innerHTML to get HTML not .Value.
  • .Value should be .value that works with input and textarea.

So Your example should work see below code:

function checkAgrmVal() {
var GridPayInfo = document.getElementById("tbl");
        for (var i = 0; i < GridPayInfo.rows.length; i++) {
            var AgrmntAmt = GridPayInfo.rows[i].cells[5].innerText;
            if (AgrmntAmt <= "0") {
                alert('Agreement amount cannot be 0 or less than 0 found in row: '+ (+i+1));
                return false;
            }
        }
    }
<table id='tbl'>
<tr><td>a</td><td>b</td><td>c</td><td>d</td><td>e</td><td>-1</td></tr>
<tr><td>a</td><td>b</td><td>c</td><td>d</td><td>e</td><td>5</td></tr>
<tr><td>a</td><td>b</td><td>c</td><td>d</td><td>e</td><td>-3</td></tr>
<tr><td>a</td><td>b</td><td>c</td><td>d</td><td>e</td><td>-4</td></tr>
<tr><td>a</td><td>b</td><td>c</td><td>d</td><td>e</td><td>3</td></tr>
</table>
<button onclick='checkAgrmVal()'>check</button>

Upvotes: 1

Related Questions