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.)
csindorfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

julio011597Commented:
If you think of it as a string, the problem can basically be solved in two ways (in Javascript):

1. check one character at a time in the string, and see it it in [0123456789];
2. use Regular Expressions (the RegExp would be [^0-9]), which, BTW, works with JS1.2 (NN4.x IE4.x).

BUT, if you instead think of it as a number with a length constraint, here is a very simple method to do the trick - i bet you'll like it :)

--//--
function checkSocialNum(aField) {
  if(aField.value == "") self.alert("Please, enter Social Number"); // it's empty
  else {
    var val = parseInt(aField.value, 10); // let's make it a number
    /* it must:
     * 1. be a valid number                       (i.e. !isNaN)
     * 2. be positive, integral, and 9 in length  (i.e. > 99,999,999 )
     */
    if(!isNaN(val) && val > 99999999) return true // it's valid

    self.alert("Invalid Social Number"); // it's not empty & not valid
  }

  // empty & invalid input "clean-up"
  aField.focus();
  aField.select();
  return false;
}
--//--
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
julio011597Commented:
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.
0
csindorfAuthor Commented:
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.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

julio011597Commented:
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.
0
csindorfAuthor Commented:
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.
0
julio011597Commented:
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.
0
csindorfAuthor Commented:
cool beans I will give it a try and see where it gets me.  Thanks a heap julio for all you have done.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.