?
Solved

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

Posted on 2004-05-02
10
Medium Priority
?
485 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

762 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