Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q39 Is there a simple way to test if a variable is a number, e.g. test for entries like "1234a6789"?

You are here: irt.org | FAQ | JavaScript | Number | Q39 [ previous next ]

There is code around (can't lay my hand on it at the moment) that can do lots of wonderful validation for numbers.

However, if it was me I would use something like the following:

<script language="JavaScript"><!--
function isan(string) {
    if (string.length == 0)
        return false;
    for (var i=0;i < string.length;i++)
        if ((string.substring(i,i+1) < '0') || (string.substring(i,i+1) > '9'))
            return false;

    return true;
}
//--></script>

The following is a more efficient method, submitted by Mike Crawford:

<script language="JavaScript"><!--
function validateInt(iString) {
    // no leading 0s allowed
    return (("" + parseInt(iString)) == iString);
}
//--></script>

The following was submitted by Lalit C

There is single function isNaN (num).
- It returns true, if the parameter is NaN (Not A Number).

eg- No. with embeded blank or characters/symbols are NaN.
- It will return a number (if its a valid number), after trimming blanks on both sides

if (isNaN (myNumber)) {
  // Not A Number
  alert('Please enter a valid no.') ;
} else {
  // A valid Number
  // do some processing with the number
}

The following was submitted by SAJEETH.K

Hmm... FAQ Knowledge Base Q39 holds valid only in case of INTEGERS. I hope the following code would work well for even floats (it also has provision for checking precision of float too, in this case 2 decimal digits). But, might be too straight forward instead of being technical. But,a working solution is what people demand..so it "WORKS"...

function validateInt(fieldValue) {
  var found=0,i=0;
  var allowedChars=".1234567890";
  for (i=0;(i<fieldValue.length) && (found==0);i++) {
    if (allowedChars.indexOf(fieldValue.charAt(i)) == -1) {
      found=1;
      alert('Error:: Field contains non-numeric. characters...');
      return false;
    }
  }

  var dotCount=0;

  for(i=0;i<fieldValue.length;i++) {
    if (fieldValue.charAt(i)=='.')
      dotCount++;
  }

  if (dotCount >1) {
    alert('Invalid field..contains more than one decimal chars');
    return false;
  } else if(dotCount==1) {
    // Check for max. precision <=2
    // GetIndex of '.'
    if ((fieldValue.length - fieldValue.indexOf('.') ) > 3) {
      alert('Limit precision to Max. 2 chars');
      return false;
    }
  }

  return true;
}

The following was submitted by Alex Ivetic

Using regular expressions to test for valid numbers:

function isValidNumber(num){
  // regular expression to test if field contains alpha
  // expand accordingly to include any other non numeric character
  isAlpha = /[(A-Z)|(a-z)]/ ;	
  if( isAlpha.test(num)) {
    // not a number, return 0
    return 0;
  }
  // num is a number, return 1
  return 1;
}

Feedback on 'Q39 Is there a simple way to test if a variable is a number, e.g. test for entries like "1234a6789"?'

©2018 Martin Webb