SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    count length of string

    Having trouble with this simple code...


    if (form.SSN.length != "9") {
    alert("SSN must be nine digits long");
    form.SSN.focus()
    return false; }


    It always displays the alert even if it does equal 9 digits long. I'm new to javascript so if this is a really simple error just go with it

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe because
    form.SSN.length is a number
    and
    "9" is a string

    Remove ""

  3. #3
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Tried that already, no luck...

  4. #4
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm obviously checking to make sure it's a valid SSN, but all of the code out there I have seen is placed in the header of the page, which I don't want. So first I'm working on making sure the value of SSN is 9 digits long, then I'm going to try and figure out (god help me ) whether they entered numberic characters or not.

  5. #5
    Original Gangster silver trophy Thing's Avatar
    Join Date
    Oct 2000
    Location
    Philadelphia, PA
    Posts
    4,708
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Figured it out. This is how I did it

    ssn = form.SSN.value
    if (ssn.length != 9) {
    alert("SSN must be nine digits long");
    form.SSN.focus()
    return false; }

    I just put form.SSN.value into a variable and used it that way. Works perfect now. Not sure why I had to do it this way, but it works.

  6. #6
    Santos L Halper Zenith's Avatar
    Join Date
    May 2002
    Location
    Finland
    Posts
    641
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by williamsba
    Figured it out. This is how I did it

    ssn = form.SSN.value
    if (ssn.length != 9) {
    alert("SSN must be nine digits long");
    form.SSN.focus()
    return false; }

    I just put form.SSN.value into a variable and used it that way. Works perfect now. Not sure why I had to do it this way, but it works.
    Because you had first this:
    form.SSN.length

    And now:
    ssn = form.SSN.value
    ssn.length

    So you only would have had to change the first code to form.SSN.value.length and it had worked fine

    -Z-


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •