Solved

How to left trim a session variable in a drop down list

Posted on 2004-05-02
10
480 Views
Last Modified: 2012-08-13
Below is an exceprt of a drop down list in a form that is validated on the server side.  If there is an error in any of the for items on the page and the user has already selected a color it will display the name of the color they already selected in the form field.  However, instead of displaying, for example "01-Blue" -- which would be confusing to the user, I want to display "Blue."  In addition I really only want to pass "01" to the database.  How do I do left("CreateColor",2) trim on the session variable.  I know there must be some easy self-evident way to do this.  Note I originally had only the alpha values in the values in the options, but then it only returned those, and, because I have several long drop downs in this form, I figured it would be easier to just trim the things.
<%
If Session("CreateColor") <> "" Then
                
                  response.write "<select name=""CreateColor"" size=""1"" class=""mono"">"
                  response.write "<option value="""
                  response.write Session("CreateColor")
                  response.write """ selected>"
                  response.write Session("CreateColor")
                  response.write "</option>"
                        "<option value=""01-Blue"">Blue</option>" & _
                        "<option value=""02-Red"">Red</option>" & _
                        "<option value=""03-Green"">Green</option>" & _
                        "<option value=""04-Pink"">Pink</option>" & _
                        "<option value=""99-Other"">Other </option></select>"                
                  Else
%> .....
0
Comment
Question by:raudi
  • 7
  • 2
10 Comments
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10973137
say Session("CreateColor") has the value "04-Pink"

mid(Session("CreateColor"),4) will yield - Pink

left(Session("CreateColor"),2) will yield - 04

I guess u can work out the rest.
:) SD
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10973194
More information about these string functions here:-

http://www.winguides.com/scripting/reference.php?category=25

:) SD
0
 
LVL 6

Accepted Solution

by:
Lord_McFly earned 500 total points
ID: 10973272
So...

Session("CreateColor") = "01-Blue"

Response.Write Right(Session("CreateColor"),Len(Session("CreateColor")) - InStrRev(Session("CreateColor"),"-"))   <----- will return Blue

...will return the colour regardless of length...

Response.Write Response.Write Left(Session("CreateColor"),InStr(1,Session("CreateColor"),"-") - 1)   <------ will return 01

..using the above method will ensure that regardless of the length of the colour name or the length of the number the right results will be returned

Session("CreateColor") = "0011100-Dark Greeny Brown"

Using normal Left() and Right() is not suitable if you do not know the lengths.




Note to sciber_dude about Mid()

Mid(string, startPOS, noChars)
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 11

Expert Comment

by:sciber_dude
ID: 10973285
@ Lord_McFly -

> Note to sciber_dude about Mid()

> Mid(string, startPOS, noChars)

Thanks for the note.

The actual syntax is Mid(string, start[, length])  and that means the length is optional (and that means there is nothing wrong with my solution - It picks up the whole string from the start position :P)

More info: http://www.winguides.com/scripting/reference.php?id=73

:) SD
0
 

Author Comment

by:raudi
ID: 10973293
Thanks for the speedy replies, although sciber_dudes response, worked, Lord McFly' accepted answer addresses the problem of unknown lengths.

R
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10973295
@ raudi -

If 99 = Other color, I assumed that you dont have more than 99 colors (and hence the color code is not greater than 2 characters). In that case, you really dont need the complicated checking. What if you had a color ...

"13-gray-black" Then Lord_McFly's solution wont work.

Hmm.. I see that you have accepted the answer.. so good luck.

:) SD
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10973297
> addresses the problem of unknown lengths.

You didnt ask for this!
:) SD
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10973306
@  raudi -

and Lord_McFly writes...

> Using normal Left() and Right() is not suitable if you do not know the lengths.

Which you didnt state as part of your original question.

:) SD

0
 
LVL 6

Expert Comment

by:Lord_McFly
ID: 10973351
sciber_dude

Thx for the correction about the Mid() - I didn't realise you could do that.

As for "13-gray-black" I would suggest a different separating character - "13|gray-black".

Cheers

McFly
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 10973369
@  raudi -

Modifying Lord_McFly's solution for multiple "-" in the Session("CreateColor"):

e.g. if Session("CreateColor") = "13-gray-black"

Response.Write mid(Session("CreateColor"),InStr(Session("CreateColor"),"-")+1) <----- will return gray-black
Response.Write mid(Session("CreateColor"),1,InStr(Session("CreateColor"),"-")-1) <----- will return 13


and for "0011100-Dark Greeny Brown"

Response.Write mid(Session("CreateColor"),InStr(Session("CreateColor"),"-")+1) <----- will return Dark Greeny Brown
Response.Write mid(Session("CreateColor"),1,InStr(Session("CreateColor"),"-")-1) <----- will return 0011100

:) SD
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JQuery Date Time picker not showing 29 129
IP API - need data... 4 23
Button to go back 3 25
Passing Parameter to Stored Procedure 4 24
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question