Form Validation: Number Entered

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?
LVL 2
mtcmediaAsked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
<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
 
hankknightCommented:
<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
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.

 
GwynforWebCommented:
<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
 
ldbkuttyCommented:
<input type="text" name="my_number" onkeyup="this.value=this.value.replace(/[\D]/g,'')" onChange="this.value=this.value.replace(/[\D]/g,'')"/>
0
 
GwynforWebCommented:
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
 
Michel PlungjanIT ExpertCommented:
Does this one qualify?

function isInt(val) {
  return (parseInt(val,10)==(val-0))
}
0
 
knightEknightCommented:
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
 
knightEknightCommented:
Michel!  Welcome back!  Long-time-no-see-post-from-u.
0
 
Michel PlungjanIT ExpertCommented:
:) Thanks
0
 
GwynforWebCommented:
Well Michel is'nt this a suprise. Keeping the expert status up I gather. Good to see you. GfW :-).
0
 
Cem TürkSenior Software EngineerCommented:
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
All Courses

From novice to tech pro — start learning today.