Forum Moderators: open
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!
/* 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]
/* 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]
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!
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]
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]