Solved

Validating numeric fields

Posted on 2000-03-06
5
197 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
  • 3
5 Comments
 
LVL 8

Expert Comment

by:xabi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks !....
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Passing special characters in http get request 7 38
Classic ASP "Generic request error" 4 31
JSON error 4 38
asp syntax 3 20
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/…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now