[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 69
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PawełCommented:
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łCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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