• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 71
  • Last Modified:

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);">
0
earwig75
Asked:
earwig75
  • 2
  • 2
  • 2
1 Solution
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
PawełSharePoint DeveloperCommented:
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
 
PawełSharePoint DeveloperCommented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now