Syed Syed - 1 month ago 6
jQuery Question

Text box should not accept 0's using jQuery

Im trying to validate the textbox that should not contain all 0's or even just 1 zero, but it can contain any alphanumeric.. condition is if we give just 0 or all 0's it should not allow.. can we just check with the val?

Here is the code

$(document).ready(function () {

$("#btnSave").click(
function () {
AlertSave();
}
);
});

function AlertSave() {
if($('#txt').val() == '0'){
alert('should not be 0')
}
}


Fiddle

http://jsfiddle.net/h4JXs/4865/

Answer

Check if all characters are '0':

if ([].every.call($('#txt').val(), _ => _ === '0')){
      alert('should not be 0')
    }