Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String parsing

Status
Not open for further replies.

dreamclutch

Programmer
Oct 3, 2005
21
US
Trying to grab an options value from a form option usnig javsascript, and then sorta out the values into two integers for a case method. If I manually enter integers in the case method it works. The issue seems to lie with either the options value syntax or substr? Thanks


//Javascript Code
//convert form options data from comma delimited line (x,x) to 2 intergers

var day1 = form1.Branch.options[form1.Branch.selectedIndex].value
var day2 = form1.Branch.options[form1.Branch.selectedIndex].value

day1.substr(0,1) //returns first interger day
day2.substr(2,3) //returns second interger day

//get day of the week for alert

//two case methods grabbing two numbers
//switch (first integer) {
//code }

//switch (second interger) {
//code }


<form name="form1" method="post" action="shipUpdate.asp" onSubmit="return check_date()">

//some other form code

//option values below (4 and 5) are parsed from a two db fields


<select name="Branch">
<option value="SelectOne" selected>Select One</option>
<option value="4,5" label="101-9th Avenue">101-9th Avenue</option>
</option>
 
try something like this:

Code:
var dayMush = form1.Branch.options[form1.Branch.selectedIndex].value.split(",");
var dayOne = parseInt( dayMush[0] );
var dayTwo = parseInt( dayMush[1] );

switch (dayOne) {
  // ...
}

switch (dayTwo) {
  // ...
}

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
have you tried using alert to see what lies in the values of day1 and day2? I think you might be surprised if you do for 2 reasons:

1. calling the substr method on a string does not change the string itself - you will have to manually overwrite the value of the variable for that:
Code:
[COLOR=red][b]day1 = [/b][/color]day1.substr(0, 1)

2. you're using the substr method incorrectly for day2. the substr method works as follows: string.substr(startingPointOfString, lengthThatYouWantToPull) - so in your pull of day2 you're trying to pull a string starting at position 2 that is 3 characters long - change it to:
Code:
[COLOR=red][b]day2 = [/b][/color]day2.substr(2, [COLOR=red][b]1[/b][/color])

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
I have this, but doesn't seem to work.


//splice form options data into comma delimited line with two intergers
var day1 = form1.Branch.options[form1.Branch.selectedIndex].value
var day2 = form1.Branch.options[form1.Branch.selectedIndex].value

day1 = day1.substr(0,1) //returns first interger day
day2 = day2.substr(2,1) //returns second interger day

//get day of the week for alert
switch (day1) {
case 0:
RunOnDay ="Sunday";
break
case 1:
RunOnDay ="Monday";
break
case 2:
RunOnDay ="Tuesday";
break
case 3:
RunOnDay ="Wednesday";
break
case 4:
RunOnDay ="Thursday";
break
case 5:
RunOnDay ="Friday";
break
case 6:
RunOnDay ="Saturday";
break
}

switch (day2) {
case 0:
RunOnDay2 ="Sunday";
break
case 1:
RunOnDay2 ="Monday";
break
case 2:
RunOnDay2 ="Tuesday";
break
case 3:
RunOnDay2 ="Wednesday";
break
case 4:
RunOnDay2 ="Thursday";
break
case 5:
RunOnDay2 ="Friday";
break
case 6:
RunOnDay2 ="Saturday";
break
}

themessage ="This report can only be run on: "
themessage += RunOnDay
themessage += RunOnDay2
alert(themessage)
return false
}
 
ahhh it works..yes cLFlaVA your method seemed to work.

- Justin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top