Validate form text box with javascript. Numeric and not blank.

I have a form text box that I need to validate. I need to validate that a number greater than zero is entered and it cannot be blank.

I thought they would be easy, but my script constantly fails.

Can someone assist? Thank you.

I am sending the number to the script like this:

<input type="button" id="myNumber" name="Submit" value="Submit" onclick="execute(document.getElementById('myNumber').value);">
Who is Participating?

Improve company productivity with a Business Account.Sign Up

PawełConnect With a Mentor I Design & Develop SoftwareCommented:
try this using regex in javascirpt

this will work with older browsers, not sure how far back, and it'll restrict to just numbers
Mukesh YadavFull Stack DeveloperCommented:
Can you please share code for function execute?
earwig75Author Commented:

My method was not working, so I gave up and deleted it.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Mukesh YadavFull Stack DeveloperCommented:
So number should me minimum 1.

<input type="button" id="myNumber" name="Submit" value="Submit" onclick="validate();">

function validate(){
    var element = document.getElementById('myNumber');
    if(parseInt(element.value) < 1){
        element.value = 1;

Open in new window

PawełI Design & Develop SoftwareCommented:
which browsers are you supporting? because html5 has a pattern attribute on the input tag
earwig75Author Commented:
I need to support old browsers and ONLY allow numeric inputs. I think Mukesh's solution would allow letters and numbers.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.