Solved

problem with using isNaN

Posted on 1998-04-08
7
559 Views
Last Modified: 2008-02-26
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
Comment
Question by:csindorf
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 20 total points
ID: 1279141
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
 
LVL 5

Expert Comment

by:julio011597
ID: 1279142
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
 

Author Comment

by:csindorf
ID: 1279143
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:julio011597
ID: 1279144
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
 

Author Comment

by:csindorf
ID: 1279145
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
 
LVL 5

Expert Comment

by:julio011597
ID: 1279146
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
 

Author Comment

by:csindorf
ID: 1279147
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now