Solved

Validating numeric fields

Posted on 2000-03-06
5
202 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

732 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