0

I'm 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

Artur Filipiak
  • 9,027
  • 4
  • 30
  • 56
Syed
  • 2,471
  • 10
  • 49
  • 89
  • You're probably better off using a regex than just checking the value. – MCMXCII Oct 26 '16 at 16:24
  • You are checking if the entire value is '0' not whether or not it *contains* a '0'. You could use regex or just do `$('#txt').val().IndexOf('0') > -1`. – nurdyguy Oct 26 '16 at 16:25

3 Answers3

0

Check if all characters are '0':

if ([].every.call($('#txt').val(), _ => _ === '0')){
      alert('should not be 0')
    }
Malk
  • 11,855
  • 4
  • 33
  • 32
0

Try using Parsley pattern tag with regex to validate your field. check out this How to use the parsley.js pattern tag?

Community
  • 1
  • 1
shadramon
  • 59
  • 8
0

Remove extra spaces, break lines, tabs

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