Solved

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

Posted on 2004-05-02
10
478 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now