list and onblur event..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aashishn86
    New Member
    • Mar 2009
    • 51

    list and onblur event..

    Hi!!
    i want to validate a list box onthe onblur event and if no value is selected , display a message on the left using <span> and innerHTML


    here's what i have written

    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function validateCB()
     {
      if(document.rec.loc.options.selectedIndex = = 0)
       {
        alert(" hi");
        return false;
       }
       else
        return true;
      }
    
    
    function validatePID(inputfield, helptext)
     {
      if(inputfield.value.length !=9)
    `  {
        if(helptext != null)
         helptext.innerHTML = "please enter excatly 9 digits";
         return false;
       }
      else
       {
         if(helptext != null)
         helptext.innerHTML = "";
         return true;
    
      }
    }
    
    </script>
    
    </head>
    
    <form method = "post" action="rec.asp" id="rec" name="rec">
    <table>
     <tr>
     <td>
      <input id='pid' name='pid' onblur="validatePID(this,document.getElementById('pid_help'))">
      <span id ="pid_help" class="help" > </span> <br> <br>
    </td>
    
    
    <td>
    <select id='loc' name='loc' onblur="validateCB()"> <span id="loc_help"> 
    <option value = ' ' selected > select location </option>
    <option value = 'abcd' selected > abcd </option>
    <option value = '1234' selected > 1234 </option>
    </td>
    </table>
    
    </form>
    </html>
    well when i was doing this at work... when i entered less than 9 digits in the PID box..it dispalyed text on the left saying please enter 9 digits.. that's what i want to happen, if a user doesn't select a value from the list as well...i tried writing the code for it..but it didnt' work... so i just wrote the alert code..which was working ..but it isnt' here...
    dont' know where am i going wrong...
    please help !!
    thank's....
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    There should not be a space between the equals signs.

    Code:
    selectedIndex = = 0

    Should be

    Code:
    selectedIndex == 0

    Comment

    • aashishn86
      New Member
      • Mar 2009
      • 51

      #3
      sorry...
      that was a typing error...
      the problem still is the same...
      please correct the code..

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        Too many errors to explain them all. Here are working versions of the functions

        Code:
        function validateCB() {
            if(document.rec.loc.options.selectedIndex == 0)   {
                alert(" hi");
        	return false;
            } else {
        	return true;
            }
        }
         
         
        function validatePID(inputfield, helptext) {
            if(inputfield.value.length !=9)   {
        	if(helptext != null) {
        	    helptext.innerHTML = "please enter excatly 9 digits";
        	    return false;
        	} else {
        	    if(helptext != null) {
        	        helptext.innerHTML = "";
        		return true;
        	     }
        	}
            }
        }

        Comment

        • aashishn86
          New Member
          • Mar 2009
          • 51

          #5
          thank's a lot..........
          now , what i want it use a funciton like validatePID inplace of validateCB on the onblur event....

          have tried...but doesn't seem to work for me...

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            What have you tried? Do you mean you want to display a message instead of an alert?

            Comment

            • aashishn86
              New Member
              • Mar 2009
              • 51

              #7
              yeah...
              the way it does for the PID....

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Show me what you've tried and I'll tell you what corrections you need to make.

                Comment

                • aashishn86
                  New Member
                  • Mar 2009
                  • 51

                  #9
                  Code:
                   function validateCB() 
                        {
                       if(document.rec.loc.options.selectedIndex == 0)   
                  	{
                           if (helptext != null) 
                  	    {
                                helptext.innerHTML = "please select atleast one value from the list";
                                return false;
                              } 
                  	     else 
                   	     {
                           	if(helptext != null) 
                  	     {
                                 helptext.innerHTML = "";
                                 return true;
                               }
                             }
                        
                   	}
                       }
                    
                    
                    
                   function validatePID(inputfield, helptext) {
                       if(inputfield.value.length !=9)   {
                       if(helptext != null) {
                           helptext.innerHTML = "please enter excatly 9 digits";
                           return false;
                       } else {
                           if(helptext != null) {
                               helptext.innerHTML = "";
                           return true;
                            }
                       }
                       }
                   }
                  this is what i am trying to achieve........ .
                  it gives the error ' is null or not an object'...
                  thank's ..

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You need to add helptext as an argument:
                    Code:
                     function validateCB(helptext)
                    Also, add an argument in the HTML when you call this function similar to the call for validatePID.

                    In the function, your brackets are not quite correct. It'd help a lot if you could align them properly:
                    Code:
                    if(document.rec.loc.options.selectedIndex == 0)   
                    {
                        if (helptext != null) 
                        {
                            helptext.innerHTML = "please select atleast one value from the list";
                            return false;
                        }
                    }
                    else 
                    {
                        if(helptext != null) 
                        {
                            helptext.innerHTML = "";
                            return true;
                        }
                    }

                    Comment

                    Working...