?
Solved

Form Validation: Number Entered

Posted on 2005-02-28
13
Medium Priority
?
231 Views
Last Modified: 2011-09-20
Hello Experts,

What is the best, and easiest way to verify that a number entered into a form by a client is numeric. Not only that, it is a positive integer (no decimal point).

Is a regular expression the best way, or how can this be done?
0
Comment
Question by:mtcmedia
[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
  • 3
  • 2
  • 2
  • +4
13 Comments
 
LVL 16

Accepted Solution

by:
hankknight earned 500 total points
ID: 13424011
0
 
LVL 32

Assisted Solution

by:Batalf
Batalf earned 500 total points
ID: 13424013
<script>
function validateNumber(){
    var formObject = document.forms[0];
    var pattern = /^\d+$/
    if(!formObject.numberValue.value.match(pattern)){
        alert('Not a number');
        return false;
    }
    return true;

}
</script>

<form onsubmit="return validateNumber()">

<input type="text" name="numberValue">
<input type="submit">
</form>
0
 
LVL 16

Expert Comment

by:hankknight
ID: 13424017
<HTML>
<HEAD>
<script language="javascript">
<!--
function CheckInteger (thisform, lower, upper)
 {
   var thisint = parseInt(thisform.year.value, 10)
   var thisfloat = parseFloat(thisform.year.value, 10)

   if (thisint != thisfloat)
        {
    alert("You didn't enter an integer!")
    thisform.year.focus()
    return false
         }
    else
        {
       if (lower <= thisint && thisint <= upper)
               { // is integer in the range?
           alert("The value " + thisform.year.value + " is OK!")
                return false // normally this should be true but it is false so the page with not reload
               }
           else {
                  if (lower == upper && lower==thisint) {   // if range is one number and this integer then OK
                     return false // normally this should be true but it is false so the page with not reload
                         }
                    else {
                       alert("The value has to be in the range, " + lower + " <= X <= " + upper)
                         thisform.year.focus()
                         return false
                             }
                      }
              }
         }
-->
</script>
</HEAD>
<BODY>
<form name="forminteger" onsubmit="return CheckInteger(forminteger, 1900, 2001)" class="normal">
Enter a Year from 1900 and 2000: <input type="text" name="year" size=6>
<p>
<input type="submit" value="Check the year"> <input type="reset" value="Clear Form">
</form>
</BODY>
</HTML>
0
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.

 
LVL 31

Expert Comment

by:GwynforWeb
ID: 13424026
<script>
function intPos(val){
 if (isNaN(val) || /[\.\-]/.test(val)) return false
 return true
}

alert('23'+' '+intPos(23))
alert('23.7'+' '+intPos(23.7))
alert('23xas.7'+' '+intPos(23.7))
</script>
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13424033
<input type="text" name="my_number" onkeyup="this.value=this.value.replace(/[\D]/g,'')" onChange="this.value=this.value.replace(/[\D]/g,'')"/>
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 13424067
this as well, a bit crisper

<script>
function intPos(val){
 return (!/\D/.test(val))
}

alert('23'+' '+intPos(23))
alert('23.7'+' '+intPos(23.7))
alert('23xas.7'+' '+intPos(23.7))
</script>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13424310
Does this one qualify?

function isInt(val) {
  return (parseInt(val,10)==(val-0))
}
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 13424473
I like to force the issue by dis-allowing non-numeric characters:

 <INPUT type="text" name="numeric"  onkeyup='this.onchange();' onblur='this.onchange();'
   onchange='this.value=this.value.replace(/[^\d]*/gi,"");' />
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 13424483
Michel!  Welcome back!  Long-time-no-see-post-from-u.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13424528
:) Thanks
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 13424666
Well Michel is'nt this a suprise. Keeping the expert status up I gather. Good to see you. GfW :-).
0
 
LVL 17

Expert Comment

by:Cem Türk
ID: 15634477
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

Split between hankknight and Batalf

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

cem_turk

EE Cleanup Volunteer
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 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