Aju
Aju

Reputation: 511

How to validate a column in an editable datatable?

Am using an editable HTML datatable on my asp.net web page .Which look like this, enter image description here

How to add validation on column Target, to receive only float values.?

Function (For enable edit):

function editRow(oTable, nRow) {
  var aData = oTable.fnGetData(nRow);
  var jqTds = $('>td', nRow);
  jqTds[0].innerHTML = aData[0];
  jqTds[1].innerHTML = aData[1];
  jqTds[2].innerHTML = '<input type="text" id="Float" class="form-control" value="' + aData[2] + '">';

  jqTds[3].innerHTML = '<a class="save-row" href="">Save</a>';
  jqTds[4].innerHTML = '<a class="cancel-row" href="">Cancel</a>';

    }

I tried to add keypress event on the textbox , but its not working.!

$('#Float').keypress(function (event) {
           if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57) && (event.which != 8)) {
               event.preventDefault();
           }
       });

Am new to jquery so please help me solve this ?

Upvotes: 3

Views: 540

Answers (1)

BabaBaluchi
BabaBaluchi

Reputation: 68

Try:

  onload =function(){ 
  var ele = document.querySelectorAll('.number-only')[0];
  ele.onkeypress = function(e) {
     if(isNaN(this.value+""+String.fromCharCode(e.charCode)))
        return false;
  }
  ele.onpaste = function(e){
     e.preventDefault();
  }
}

Note: Above code doesn't work for -ve values.

Upvotes: 1

Related Questions