?
Solved

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

Posted on 2016-08-10
6
Medium Priority
?
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

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 9

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 To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
LVL 10

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 10

Accepted Solution

by:
Paweł earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 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