Solved

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

Posted on 2016-08-10
6
33 Views
Last Modified: 2016-08-11
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
Comment
Question by:earwig75
  • 2
  • 2
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Mukesh Yadav
ID: 41750349
Can you please share code for function execute?
0
 

Author Comment

by:earwig75
ID: 41750362
Mukesh,

My method was not working, so I gave up and deleted it.
0
 
LVL 6

Expert Comment

by:Mukesh Yadav
ID: 41750381
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:Paweł
ID: 41750448
which browsers are you supporting? because html5 has a pattern attribute on the input tag
0
 

Author Comment

by:earwig75
ID: 41750534
I need to support old browsers and ONLY allow numeric inputs. I think Mukesh's solution would allow letters and numbers.
0
 
LVL 8

Accepted Solution

by:
Paweł earned 500 total points
ID: 41750535
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
date format angularjs2 16 36
dynamics crm 2011 - Web Resources 1 24
JavaScript Scope issue 4 18
Re-position the objects 7 49
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now