Negative Numbers validation using JavaScript & jQuery

When you need to restrict user to enter negative numbers in input text field,
You can achieve using plain JavaScript & jQuery as explained in below video.

It will also not allow user to enter alphabet letters like A to Z.

Please leave your suggestions & comments if you found above code useful, so when other person read this blog, he/she can use above code with confidence.

Please subscribe my YouTube channel for more technical videos: https://www.youtube.com/c/CodeCanvas/

8 thoughts on “Negative Numbers validation using JavaScript & jQuery

  1. Its not working for negative numbers.
    For that we have to put ‘OR’ condition instead of ‘AND’ condition as follows :

    value = document.getElementById(‘ID’).value;

    if (parseInt(value) < 0 && isNaN(value))
    {
    alert("please enter valid values");
    document.getElementById('iD').value = ' ';
    document.getElementById('iD').focus();
    }

    Like

    • Hello subha,

      Thanks for your feedback.
      We cant put if (parseInt(value) < 0 && isNaN(value)) because if one of 2 condition will not satisfy , then it will not validate.

      Like

I like to hear from you about this !!

This site uses Akismet to reduce spam. Learn how your comment data is processed.