Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

problem with using isNaN

Posted on 1998-04-08
7
Medium Priority
?
580 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
julio011597 earned 80 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

719 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