Solved

Validating numeric fields

Posted on 2000-03-06
5
200 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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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 would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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