Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Validating numeric fields

Posted on 2000-03-06
5
Medium Priority
?
205 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 200 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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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