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/
Saved as a favorite, I really like your website!
LikeLike
What’s up, just wanted to mention, I enjoyed this blog post.
It was helpful. Keep on posting!
LikeLike
Using ‘OR’ is correct ,otherwise it shows alert only for negative strings.
LikeLike
Thats true..!!!
LikeLike
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();
}
LikeLike
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.
LikeLike
Dear Nilesh,
Thanks For your Feedback..
LikeLike
its relay work
LikeLike