SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot carlos_10111's Avatar
    Join Date
    Jun 2006
    Location
    SOUTH AFRICA
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question need help with validation

    Hi guys!
    It's my first time on this forum and new to javascript too.
    i've trying to do just a simple validation on a form but, i get cought up in a kind of endless loop that can't finish.
    here is my code:
    HTML Code:
    <script language="javascript1.2" type="text/javascript">
    <!--
    function validate_firstname(field1){
    if(document.frmtest.names.value==""){
       window.alert('please fill in the name');
       document.frmtest.names.focus();
    }
    }
    
    
    function validate_surname(field2){
    if(document.frmtest.surname.value==""){
       window.alert('please fill in the surname');
       document.frmtest.surname.focus();
    }
    }
    
    //-->
    </script>
     
    
    <form id="form1" name="frmtest" method="post" action="">
      <label>
      <input type="text" name="firstname" onblur="validate_firstname(this)"/>
      </label>
      <p>
        <label>
        <input type="text" name="surname" onblur="validate_surname(this)" />
        </label>
      </p>
    </form>
    As u can see guys, all i want to do is, when some one press tab and didn't fill in a field, then a warning message must show up and, when u click ok it must take u back to the same textfield until u type something.
    It's somehow working but, i get both functions being called in some king of an endless loop!

    Please help out!
    Carlos.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Code:
    function validate_firstname(field1)
    {
      if(document.frmtest.names.value==""){
         window.alert('please fill in the name');
         document.frmtest.names.focus();
      }
    }
    1) names isn't in the form anywhere, so document.frmtest.names won't work
    2) You have the element (through this) already, so:

    Code:
    if(field1.value=="")
    {
    and that will produce what you want.

  3. #3
    SitePoint Zealot carlos_10111's Avatar
    Join Date
    Jun 2006
    Location
    SOUTH AFRICA
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry

    HTML Code:
    function validate_firstname(field1)
    {
      if(document.frmtest.firstname.value==""){
         window.alert('please fill in the name');
         document.frmtest.firstname.focus();
      }
    }
    I made a mistake with names, it supose to be firstname. still i rewrote the function like this:
    HTML Code:
    function validate_firstname(field1)
    {
      if(field1value==""){
         window.alert('please fill in the name');
         field1.focus();
      }
    }
    
    function validate_surname(field2)
    {
      if(field2.value==""){
         window.alert('please fill in the name');
         field2.focus();
      }
    }
    And then i call these functions like this:
    HTML Code:
    <input type="text" name="firstname" onblur="validate_firstname(this)"/>
    <input type="text" name="surname" onblur="validate_surname(this)" />
    The problem is still, when i don't put anything in the first textbox, i get both error messages at once without going to second editbox! And then, when i click ok, it displays one message and, when i click again it calls the other message!
    The only way to get out of this mess is to close the browser!

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if(field1value==""){ <--- where's the .?


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
  •