#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    USA
    Posts
    140
    Rep Power
    12

    Javascript Form Validation isn't happening


    I'm pulling out my hair trying to figure out why my javascript form validation is not working. It's most likely something very simple that I'm overlooking, as this is my first serious run at js.

    Here's my experimental code:

    PHP Code:
    <script language="JavaScript" type="text/javascript">

    function 
    ValidateForm(form)
    {
       if(
    IsEmpty(form.fname)) 
       { 
          
    alert('You have not entered a first name'
          
    form.fname.focus(); 
          return 
    false
       } 
     
    return 
    true;
     
    }

    </script>

    <form name=form action=test.php method=post onsubmit="return ValidateForm(this);">
    <input type=text size=10 name=fname>
    <br><br>
    <input type="submit" name="submit" value="Submit">
    </form> 
    When I click submit, it goes on to the test.php page, even when the fname field is blank. What am I missing?
  2. #2
  3. No Profile Picture
    The New User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    109
    Rep Power
    11
    First, you do not access form.fname. In Javascript, you need to access form.fname.value , a simple switch. Second, in Javascript, I do not think IsEmpty will fit your need. I used the following code, and it worked:
    Code:
    if(form.fname.value==""){
    ...
    }
    Hope that helps.

    --Neil
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    USA
    Posts
    140
    Rep Power
    12
    Hm. That works.

    Most of the java tutorials on form validation used the IsEmpty() function. Oh well.
  6. #4
  7. No Profile Picture
    The New User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    109
    Rep Power
    11
    Well... if you do use isEmpty(), notice the non-capitalized i ... Most programming languages (JS, PHP, etc) make predefined functions start with a lower-case letter as far as I know.

    --Neil
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    The Netherlands
    Posts
    122
    Rep Power
    11
    Also, don't use 'form' as a variable name. It is not a javascript reserved word but it is a html word and should not be used as a variable name. Get used to good habits from the start and it will make life easier in the long run!

IMN logo majestic logo threadwatch logo seochat tools logo