SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Was working now isn't (Form Validation)...

    Hi,

    I've had the following code working for months, now with apparently no changes to the script it's suddenly giving me the following error:

    Error: 'document.data.card_number.value' is null or not an object

    Code:
    <SCRIPT LANGAUGE="JavaScript">
     function checkcard_number(){
     
     if (document.data.card_number.value.length != 19 + isNaN(document.data.card_number.value))
     { 
     alert("Card Number Must be 19 Digits") 
     return false 
     }
     else
     return true
     }
     </script>
    On Submit:
    Code:
     <FORM NAME="data" METHOD=POST ACTION="my_script.asp" onSubmit="return checkcard_number(this)">
    Can anyone see anyhting?

    Thanks

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function checkcard_number(oForm)
    {
       if (!/^\d{19}$/.test(oForm.card_number.value))
       { 
          alert("Card Number Must be 19 Digits")
          oForm.card_number.focus(); //optional
          oForm.card_number.select(); //optional
          return false 
       }
       else return true;
    }
    Using isNaN() will incorrectly pass floating-point numbers. One regex does it all.

    btw: the logical OR operator in JS is ||.
    ::: certified wild guess :::

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much.


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
  •