Link to home
Start Free TrialLog in
Avatar of csindorf
csindorf

asked on

problem with using isNaN

Is there a simple way to make sure that a number is a whole number?  I am making sure that a user enters their social and the(! isNaN) will accept 123456.89 as a length of nine number.  I was trying to use the parseInt but I could not validate the length.  parts of my code is below with the loctions of my problem.  I have the code to check the characters in the string for numbers so I o not need that I was wondering if there was another way.TIA

function checkSocialNum(aForm)
{
          var socNumF = aForm.Social_Number;  
          var numberIn = socNumF.value;
          var WholeNumberChk = parseInt(numberIn);
      
        alert(WholeNumberChk.value.length); ( I get an error here that WholeNumberChk.value has no properties)
        alert(WholeNumberChk.length)            ( returns an error of undefined.)
ASKER CERTIFIED SOLUTION
Avatar of julio011597
julio011597

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of julio011597
julio011597

BTW, your code has two problems:

1. "parseInt(aString)" does not specify a base to convert to, so if your input, by chance, starts with - say - "0x...", it will be interpreted as an hexadecimal.

2. "var WholeNumberChk = parseInt(numberIn)" creates a Number object, which has no "value" or "length" properties.
Avatar of csindorf

ASKER

I tried the above and 000000000 and 111111111 both come back as invalid number so I think I will go with the string search.  I do not have any data on the RegExp so I do not know how to even try that one.  And it took me a while of playing but I discovered what you said about parseInt the hard way but now I know.
While 111,111,111 should work (??), i didn't think of numbers starting with zeros, so my answer is unuseful.

I'll write the right - and usual - answer in a few hours.
In general, as a suggestion, don't accept an answer if it does not solve your problem.
I acceptd the answer because it did solve some problems even if it was not directly related to this one directly.  This project is more than just this one so it did help in another area.
Ok.

Here is the code:

--//--
      function checkSN(aField) {
        var isLEN = 9;
        var val = aField.value;

        if(val == "") self.alert("Enter Social Number");
        else {
          if(val.length == isLEN) {
            var i;
            for(i=0; i<isLEN; i++)
              if(isNaN(val[i])) break;
            if(i == isLEN) return true;
          }
          self.alert("Invalid Social Number");
        }

        aField.focus();
        aField.select();
        return false;
      }
--//--

The isLEN var is intended as a sort of *constant* for better maintenance - so the conventional uppercase -, that is if you need to reuse the same function on a different size, you just need to change isLEN.

You could also use checkSN() as a general purpose *number string* check function by making isLEN a parameter - i'm thinking of ZipCodes or the like -:

function checkNumField(aField, aLen) {
  var val = aField.value;
  ...
    if(val.length == aLen) {
      ...
}

Cheers.
cool beans I will give it a try and see where it gets me.  Thanks a heap julio for all you have done.