There is a simple form with checking numeric data through a regular expression through the following function:

function filterFloat (value) { var floatTemplate = /^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/; if( floatTemplate.test(value) ){ return Number(value); } alert ('Неверное значение!'); } 

When you try to send a zero, nothing happens at all - how to fix it?

  • one
    Your function processes the string 0 correctly and returns the number zero. Perhaps you continue to check incorrectly built. - Visman
  • filterFloat(0) === 0 - will be true . So the error is most likely where filterFloat is filterFloat . - saaaaaaaaasha
  • Yes, the problem was that the result of the check was assigned to a variable, and the variable equal to zero was obtained false - Leo240
  • @ Leo240 If the problem is solved, please write the answer yourself. - higimo

1 answer 1

The problem was that the result of the check was assigned to a variable, and a variable equal to zero returns false.

Solution: added a check for equality to zero in the condition

 var quantityValue = filterFloat( $('#quantity').val() ); if(quantityValue || quantityValue === 0){ //дальнейшие действия } 
  • it is better to limit it to one test for null / undefined : if(quantityValue != undefined){ - Grundy