Syed Syed - 26 days ago 7
jQuery Question

Total textbox value should not be zero using jquery

Im checking the textbox value should not accept zero.. its working if I give all 0's or a single 0. But, when I give value such as 00010, its also validating.. which it should not. I'm using indexOf to test the field value.

The total value of the textbox should not be 0.

here is my code

function AlertSave() {
var val = $('#txt').val().replace(/[\s\n\r]/g,"");
if( val == 0 || val.indexOf("0") > -1){
alert('should not be 0')
}
}


A simple fiddle

http://jsfiddle.net/h4JXs/4900/

Answer

The code you need is this:

function AlertSave() {
   var val = $('#txt').val().replace(/[\s\n\r]/g,"");
      if( val == 0 && val!=''){
       alert('should not be 0')
      }
}

JSFiddle: http://jsfiddle.net/h4JXs/4903/

Comments