Welcome to WebmasterWorld Guest from 35.173.234.237

Forum Moderators: open

Message Too Old, No Replies

Importing data into a function.

multiple values per variable set?

     
1:11 am on Jul 10, 2007 (gmt 0)

New User

10+ Year Member

joined:July 10, 2007
posts:2
votes: 0


I am working on a form that needs to check the values of two fields when submitting. If the submitted values equal a set of stored values, then one URL should be returned; if the submitted values do not equal the stored values, then a different URL should be returned.

This would be easy if the number of stored values was small (I could write an 'if' statement with a series of 's), but the amount of data the field needs to check is massive. In other words, I need a way to house all of this data in 3 variables (2 variables for one field, 1 variable for the other field).

This is what I am working with:

-----------------------------------

x = 1
y = 100
z = "1st street"

function checkForm() {
if ((document.form.number.value >= x) && (document.form.number.value <= y) && (document.form.street.value == z)){
top.location.href="example.com/net/org";
document.form.number.focus();
return false;
}
else {
top.location.href="example2.com/net/org";
return false;
}
}

-----------------------------------

So, at the moment, when checkForm() is called, the function works beautifully - but I need my variables to be more powerful!

For instance, if I used checkForm(x,y,z), how would I try:

checkForm(1,100,"1st street")
checkForm(350,450,"21st street)
checkForm(75,220,"14th avenue")

...and so on and so forth? How would I do this while skipping over and ignoring the non-matches?

I apologize for the funny phrasing. Coding is very new to me so I am afraid I do not know all of the official wordings! If anything is unclear, please let me know, and I will try again.

Thanks in advance!

2:05 am on July 10, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


This is a case for an array. A 2-D array, in fact:

/* Swap corrupted ¦ chars for pipes */

var redirects = { 
found: "example.com/net/org",
notFound: "example2.com/net/org"
}

var records =
[

[1,100,"1st street"],
[350,450,"21st street"],
[75,220,"14th avenue"] /* last memb. No comma! */
]

/* Pass the form itself in the onsubmit
* onsubmit="return checkForm(this)"
*/
function checkForm(form){
/*! Haven't included a check for "0x.." form hexadecimal */
var number = form.number.value;
/* Trim w/space & normalise string*/
var street = form.street.value.toLowerCase().replace(/^\s+¦\s+$/g,'');
var redirect = redirects.notFound;

for(var k=-1,rec; rec=records[++k];){
if( number>=rec[0] && number<=rec&& street==rec[2] ){
redirect = redirects.found;
break;
}
}

top.location.href=redirect;
return false;
}

[1][edited by: Bernard_Marx at 2:12 am (utc) on July 10, 2007]

2:14 am on July 10, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


Sorry. Being plagued by a little editor parsing bugs.

/* Swap corrupted ¦ chars for pipes,
and *one* for 1
*/

var redirects = { 
found: "example.com/net/org",
notFound: "example2.com/net/org"
}

var records =
[

[1,100,"1st street"],
[350,450,"21st street"],
[75,220,"14th avenue"] /* last memb. No comma! */
]

/* Pass the form itself in the onsubmit
* onsubmit="return checkForm(this)"
*/
function checkForm(form){
/*! Haven't included a check for "0x.." form hexadecimal */
var number = form.number.value;
/* Trim w/space & normalise string*/
var street = form.street.value.toLowerCase().replace(/^\s+¦\s+$/g,'');
var redirect = redirects.notFound;

for(var k=-1,r; r=records[++k];){
if( number>=r[0] && number<=r[*one*] && street==r[2] ){
redirect = redirects.found;
break;
}
}

top.location.href=redirect;
return false;
}

[edited by: Bernard_Marx at 2:18 am (utc) on July 10, 2007]

1:44 am on July 11, 2007 (gmt 0)

New User

10+ Year Member

joined:July 10, 2007
posts:2
votes: 0


That was a tremendous help! Thank you, Bernard Max. I got everything working properly, but now I have run into another roadblock.

How would I perform the same type of procedure again on "var street"? In other words, how would I set up something like this:

var street =
[
[var west_34th_street = ["west 34th street","west 34th st","w 34th street","w 34th st"]],
[var west_35h_street = ["west 35th street","west 35th st","w 35th street","w 35th st"]],
[so-on, so-forth...]
]

I am clueless when it comes to loops, so I am still trying to wrap my head around the first solution. Any insight into this second solution will, I think, help me to understand the nature of these things.

Also, is there a much easier way to do this that I am not considering? Thanks!

9:28 pm on July 11, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


I'm guesing that you need to map as many possible spellings to one street (or
record
above).

Here's your approach. I'm using an object (of type,

Object
), good for mappings of this type. Each "variable" is a propertyName (or "key") of the object. We enumerate the keys using a
for..in
loop (Note that the loop is spitting out the key strings, not the arrays themselves).

The function returns a string, or null, if no match was made.

function findKey(sInput){ 
sInput = sInput.toLowerCase();
for(var key in street){
var spellings = street[key];
for(var k=0,L=spellings.length; k<L; k++){
if(spellings[k]==sInput)
return key;
}
}
return null;
}

var street =
{
west_34th_street:["west 34th street","west 34th st","w 34th street","w 34th st"],
west_35h_street :["west 35th street","west 35th st","w 35th street","w 35th st"]/*!NC*/
}

/* A test */
alert(findKey("west 35th st")) /* --> "west_35h_street" */

[edited by: Bernard_Marx at 9:33 pm (utc) on July 11, 2007]

9:38 pm on July 11, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


..but there is a variation that may be a little better. It uses regular expressions instead of an arrays to match the inputs. Less typing for one thing, but it also allows for thing like extra spaces between each part of the input. It is also more scaleable - considering all the possible combinations that could occur.

RegExp special chars:

^ - begins
? - perhaps one
+ - at least one


var street =
{
west_34th_street: /^w(est)? +34(th)? +st(reet)?/,
west_35th_street: /^w(est)? +35(th)? +st(reet)?/ /*!*/
}

function findKey(sInput){
/* Normalise.
- so no need to put "i" flag on all expressions */
sInput = sInput.toLowerCase()
for(var key in street)
if(street[key].test(sInput))
return key;
return null;
}

/* A test */
alert(findKey("west 35 st")) /* --> "west_35h_street" */

[edited by: Bernard_Marx at 9:42 pm (utc) on July 11, 2007]

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members