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);">
earwig75Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
PawełConnect With a Mentor I Design & Develop SoftwareCommented:
try this using regex in javascirpt
http://codepen.io/anon/pen/EyGgod

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

My method was not working, so I gave up and deleted it.
0
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();">

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

Open in new window

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