Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

try to get length after rtrim

Status
Not open for further replies.

cat5ive

MIS
Joined
Dec 3, 2004
Messages
184
Location
US
Hi,

I want to validate the drop down box selection. The value for each item is either 1 char(title), 7char(name), or equal to "none". The selection is good only when user select on the name (which value contain 7 characters). So I try to trim it then find the length. If the length is not 7 then pop up the alert box.

I tried the code below but it's not working. When I click submit, I got error message "object doesn't support this property or method". When I click 'ok' to let it debug, then it highlight at this statement.

str.charAt(i)==" ";

Thanks in advance

Here is the drop down box.
Code:
 <select name="vendor" class="ver11pxblk">
		           <option value="none" selected>Select One </option>				  
		           
                      <option value="F"> --------- FREIGHT -------------</option>
				   
                      <option value="FF01010"> ABC Co.</option>
				   
                      <option value="FF01020"> APL Co. </option>  
                    				   
                      <option value="S"> --------- SERVICE -------------</option>
				   
                      <option value="SS01010"> UPS </option>
				   
                      <option value="SS01020"> DHL </option>
				   
                   </select>
Here is my validation
Code:
  function rTrim(str)
	    {
	      if (str==null)
		  {return null;}
	      for(var i=str.length-1;str.charAt(i)==" ";i--);
	      return str.substring(0,i+1);
	    }
  function check_input()
  {
    // some other input validation 

   if (rTrim(document.f1.vendor).length != 7)		 
		   {
		     alert ("Please select Issuer");			 
			 return false; 
		   }

   // some other input validation 
   }
 
I'd guess that you're sending a zero-length string (which is NOT null) and the script is choking on str.charAt(-1).

Lee
 
>>you're sending a zero-length string

How can it be zero? Each of the item in the drop down box has value (please see the drop down box above). Or do you mean the way I code the validation is not right?

Thanks
 
I was looking only at the rTrim function, and not the function that calls it. The error is that you're trying to perform a trim on the select itself, and not using the [red]selected index[/red] of the [red]options[/red] array and trimming the [red]value[/red] of that.

Code:
if (rTrim(document.f1.vendor).length != 7)

Lee
 
I think I found it. Probably should be something like this.

if ((rTrim(document.f1.vendor.value)).length != 7)

Will test tomorrow at work. Thank anyway for your help, trollacious.
 
Getting closer, but not there yet. Look at the things I wrote in red in my previous post.

Lee
 
Ok, I just saw your latest reply. So it seem like what I found is not going to fix the problem! Is it possible that you fix the code for me?

Thanks
 
How about this?

Code:
var idx = document.f1.vendor.selectedIndex;
if (rTrim(document.f1.vendor.options[idx].value)).length != 7)
 
It's working now, with the code on my previous post (except I got extra ')' in there).

Thanks you Lee.
 
Hi,

How can I put the highlight in drop down box when user did not select the correct item?

Thanks
 
You'd use
Code:
document.f1.vendor.options[NumberThatShouldHaveBeenChosen].selected = true;

Lee
 
Well, that's not exactly what I'm looking for. User may select any item on the menu as long as it's not title or the first item.

Code:
They cann't not select any of these
<option value="none" selected>Select One </option>                  
option value="F"> --------- SERVICES -------------</option>                   
<option value="F"> --------- FREIGHT -------------</option> 

They may select any of these.
  <option value="FF01010"> ABC Co.</option>                   
<option value="FF01020"> APL Co. </option>
So if user select 'SERVICES', then I want to pop up the alert box and also highlight on 'SERVICES'. So it would be easier for user to see where the mistake is because there are many fields on the form.
I'm thinking about changing the backgroud color (if there is no easier/better way.) but I don't know how to change it back after user select the correct item.

Thansk
 
My last post was not clear enough.

When I select on any item on the drop down menu, the item get highlighted. However when I move the cursor to the next field, the highlight is gone. At that time the alert box won't come up yet (if I select the wrong item) since I handle all the error checking at the time when the submit button get click.
Probably the other way to solve my problem is to put event handler to check my drop down menu separately. Which way is better? Please suggest. I just want to do the right way not just the working way.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top