?
Solved

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

Posted on 2004-05-02
10
Medium Priority
?
489 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

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