Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

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.)
0
csindorf
Asked:
csindorf
  • 4
  • 3
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now