Solved

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

Posted on 2004-05-02
10
481 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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