Solved

Validating numeric fields

Posted on 2000-03-06
5
203 Views
Last Modified: 2008-03-03
Hi,

Is there a pre-defined jscript function that I can use to ensure that the user enters a numeric value for a particular field?

I know in Java you can use isDigit() - though this doesn't seem to be available to javascript.

Any ideas?

Andy
0
Comment
Question by:amtate
[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
5 Comments
 
LVL 8

Expert Comment

by:xabi
ID: 2587159
There is no function in javascript, so here is how I do it:

// Regular expression version
function is_integer(number) {
      var reInteger = /^\d+$/
      return reInteger.test(number)
}

// Standard version
function is_integer(number) {
 for (i=0; i<number.length; i++) if ((number.charAt(i)<0) || (number.charAt(i)>9)) return false
 return true
}

xabi
0
 
LVL 8

Expert Comment

by:xabi
ID: 2587200
Sorry, i did a typo:
Here goe sthe correct one:

function is_integer(number) {
 for (i=0; i<number.length; i++) if ((number.charAt(i)<"0") || (number.charAt(i)>"9")) return false
 return true
}

0
 
LVL 8

Expert Comment

by:xabi
ID: 2587208
function is_number(number) {
      var reReal = /^\d+\.?\d?$/
      return reReal.test(number)
}

function is_number(number) {
 iTmp = 0
 if ((number.charAt(iTmp)<"0") || (number.charAt(iTmp)>"9")) return false
 iTmp = 1
 for (i=iTmp; i<number.length; i++) {
  if (((number.charAt(i)<"0") || (number.charAt(i)>"9")) && (number.charAt(i)!=".")) return false
  if (number.charAt(i)==".") {
   i++
   break
  }
 }
 for (;i<number.length; i++) if ((number.charAt(i)<"0") || (number.charAt(i)>"9")) return false
 return true
}
0
 
LVL 10

Accepted Solution

by:
makerp earned 50 total points
ID: 2587212
isNaN(value) returns true if value is not a legal number.

if(!isNaN(document.myform.myfield.value))
{
     alert("thanx for the number!");
}
else
{
     alert("Please enter numbers only !");
}

parseInt(string,base) converts string to integer with base of base

parseInt return 0 on windows platform on error and NaN on other platforms
0
 

Author Comment

by:amtate
ID: 2587235
Thanks !....
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

688 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