Hi All ,
How validate textbox using Javascript ? condition is only allow
Positive Number -> 45
Negative Number -> -45
Thanks in advance ,
Karthik.K
Hi All ,
How validate textbox using Javascript ? condition is only allow
Positive Number -> 45
Negative Number -> -45
Thanks in advance ,
Karthik.K
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Daniel WrightPosted May 8, 2025, 3:25 AM
Hi Karthik.K,
To only allow positive and negative numbers in a textbox using JavaScript, you can utilize a validation function that ensures the input is numeric and meets your specified criteria. Here is a simple example of how you can achieve this:
In this script, the `validateNumberInput` function restricts the input to only allow digits (numbers) and a single minus sign for negative numbers. You can attach this function to your textbox input field like this:
By binding this function to the `onkeypress` event of your textbox, it will prevent users from entering anything other than numbers and a single minus sign for negative numbers.
Feel free to adjust the validation logic further based on your specific requirements, such as handling decimal numbers or refining the formatting of negative numbers. I hope this helps you achieve the desired validation for your textbox. If you have any more questions or need further assistance, feel free to ask!