Solved

# Form Validation: Number Entered

Posted on 2005-02-28
Medium Priority
231 Views
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
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
• 3
• 2
• 2
• +4

LVL 16

Accepted Solution

hankknight earned 500 total points
ID: 13424011
0

LVL 32

Assisted Solution

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

}
</script>

<form onsubmit="return validateNumber()">

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

LVL 16

Expert Comment

ID: 13424017
<HTML>
<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)
{
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>
<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

LVL 31

Expert Comment

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

</script>
0

LVL 32

Expert Comment

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

ID: 13424067
this as well, a bit crisper

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

</script>
0

LVL 75

Expert Comment

ID: 13424310
Does this one qualify?

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

LVL 33

Expert Comment

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

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

LVL 75

Expert Comment

ID: 13424528
:) Thanks
0

LVL 31

Expert Comment

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

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

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
Course of the Month12 days, 4 hours left to enroll