debug this code : validate form

Discussion in 'JavaScript' started by googlehelper, Feb 20, 2009.

  1. #1
    <!-- JavaScript -->
    <script type='text/javascript'>
    function validate(form)
    {
    var flen = document.getElementById('F1').value.length;
    var llen = document.getElementById('f2').value.length;

    if (flen<3||llen<1)
    {
    alert('Error : Enter a valid Name.Enter your initials if you dont have a last name');
    return false;
    }
    return true;

    }
    </script>
    <!-- JavaScript End-->


    <form id='form' name='join' class='joinform' enctype='multipart/form-data'
    method='post' onsubmit='return(validate(this));' autocomplete='off' >

    <ul>
    <li id='fg0'>
    <label class='desc' >Name<span class='req'>*</span></label>
    <span><select id='F0' name='sex' tabindex='2' >
    <option value='0' selected='selected'>&nbsp;Title&nbsp;&nbsp;&nbsp;&nbsp;</option>
    <option value='m'>Mr</option> <option value='f'>Ms</option>
    </select>
    <label for='F1'>Title</label></span>
    <span><input id='F1' name='fnam' type='text' maxlength='15' size='8' tabindex='3' />
    <label for='F1'>First</label></span>
    <span><input id='F2' name='lnam' type='text' maxlength='15' size='12' tabindex='4' />
    <label for='F2'>Last</label></span>
    </li>
    ........................................
    .................................


    Debug this code i dont get any alerts
     
    googlehelper, Feb 20, 2009 IP
  2. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The id on one element is F2 but you try to access it as f2.

    Javascript is case sensitive so it fails.

    this will work
    var llen = document.getElementById('F2').value.length;
    Code (javascript):
    regards
     
    gnp, Feb 20, 2009 IP
  3. googlehelper

    googlehelper Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i dont know what was the error but when i retyped the dito code line by line it worked

    anyways thanks i know that(case sensitive)
     
    googlehelper, Feb 20, 2009 IP
  4. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    if you notice on my post (and your original code)

    you where retrieving the element using a small-case f instead of the capital F (which you used for the id of the element..)

    that is why i mentioned it ... it was the actual error..

    glad you finally got it working :)
     
    gnp, Feb 20, 2009 IP