Results 1 to 2 of 2

Thread: Unstoppable Form Validation

  1. #1
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unstoppable Form Validation

    Hi..guys! I have a function here to validate null selection of drop down menu and radio button.However,it seems like alert box couldn't be stopped display even though both fileds were already selected.Thanks for your generious help

    Code:
    <script language="JavaScript" type="text/JavaScript">
    function nullValidation() {
        var x=document.getElementById("movieList");
        for (var i=0;i<x.length;i++)
        {
          if(x.elements[i].checked=="" || x.elements[i].selectedIndex == 0) 
          {
            alert("Please select "+x.elements[i].title+".");
            x.elements[i].focus();
            return false;
          }
        
        }
    return true;
    }
    </script>
    Code:
    <form action="" method="post" name="movieList" 
    onSubmit="return nullValidation();">

  2. #2
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Problem resolved.

    Code:
    <form action="" method="post" name="movieList" 
    		  onSubmit="return validate(this);">
    
    <script language="JavaScript" type="text/JavaScript">
        function validate(which) {
            var selects = which.getElementsByTagName('select');
            var radios = which.getElementsByTagName('input');
            for(sel = 0; sel < selects.length; sel++) {
                if(selects[sel].options[selects[sel].selectedIndex].text == 'Select') {
                    alert('error [select]');
                    return false;
                }
            }
    
            var rselCount = 0;
            for(radio = 0; radio < radios.length; radio++) {
                if(radios[radio].checked) {
                  rselCount++;
                }
            }
    
            if(rselCount == 0) {
               alert('error [radio]');
               return false;
            }
            return true;
        }
                    
    </script>

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
  •