Hi,
I have created a validation file in js that will direct users to another page if the link they clicked on is stored on the file however I need the if function contained in the file to work with arrays or a list of some sort
thnx in advance here is the code I have been working with - <!--
-
function validate(url){
-
outlink = url
-
str0 = 'page1.htm'
-
str1 = 'page2.htm'
-
str2 = ''
-
if ( outlink == str0 || outlink == str1 || outlink == str2 ){
-
outlink = 'error.htm'
-
}
-
else{
-
outlink = outlink
-
}
-
window.location.href=(outlink)
-
}
-
// -->
12 1816 phvfl 173
Recognized Expert New Member
Hi Wangers,
The way I would personally do this would be to extend the Array js object through use of the prototype object and add a contains method which returns true if the array contains the string passed in: -
-
Array.prototype.contains=function($str){
-
//declare a boolean set to false
-
var $blFound = new Boolean();
-
-
//loop through all items in the array
-
for (var i = 0;i<this.length;i++){
-
//check if the string for comparisonis in the item in the array.
-
if($str==this[i]){
-
//if it is change the boolean to true.
-
$blFound=true;
-
}
-
}
-
//return the value of the boolean.
-
return $blFound;
-
}
-
-
If this is in your code then all Array objects would have a method called contains which would take a string argument and return true if it matches. Your code could then be rewritten as: -
function validate(url){
-
outlink = url
-
//declare array, declare explicit size if desired.
-
var pages = new Array()
-
-
//populate from somewhere...
-
-
//check array and if it contains outlink change the value of outlink.
-
if (pages.contains(outlink)){
-
outlink = 'error.htm'
-
}
-
window.location.href=(outlink)
-
}
-
I have removed the else statement as this does not add functionality as it just assigns the value of outlink to itself. The contains is case sensitive as it stands, if you was to add a flag for case insensitivity then this could be done. Let me know if this is the case and you struggle to do this.
phvfl 173
Recognized Expert New Member
I want case insensitivity
Quote from PM
Case insensitivity can be obtained by using a regular expression (regexp) with a i (case insensitive) flag. The other flags available on regexp is g (global) which matches all instances of a string and m which causes multiline to be used.
Anyway, to put case insensitivity in this case change the contains function to: -
Array.prototype.contains=function($str){
-
var $blFound = new Boolean();
-
var $reg = new RegExp("^" + $str + "$", "i")
-
for (var i = 0;i<this.length;i++){
-
if(this[i].match($reg)){
-
$blFound=true;
-
}
-
}
-
return $blFound;
-
}
-
The new line creates a regular expression to test against using the match function in the if loop. The "^" forces the expression to match with the start of a line, the "$" forces the expression to march the end of the line (this forces an exact match) I hope this explains:
Pattern
Uses "est" "tes" "test"
None true true true
^ false true true
$ true false true
^ and $ false false true
Let me know if you want more info on this.
BTW this is a basic use for regexp as it can be used for much more such as testing is a string is a valid email pattern or postcode.
thank you for all of your help the script works great
pbmods 5,821
Recognized Expert Expert
Changed thread title to better describe the problem.
phvfl 173
Recognized Expert New Member
displays the error page no matter what values I enter into the array even if I put no value at all in there
please assist
I put this into the populate area earlier and I was wondering whether or not I was right in doing so -
pages = new Array('page1.htm');
-
pages = new Array('page2.htm');
-
You are declaring an new array on each line at the moment. To add multiple pages to the use: -
var pages = new Array();
-
pages[0]="page1.htm"
-
pages[1]="page2.htm"
-
pages[2]="page3.htm"
-
Just increment the index each time. The compare function code will need to be on each page that uses it, and will return true if a match is made.
If you continue to have problems please post the javascript that you are using so that I can check through it.
gits 5,390
Recognized Expert Moderator Expert
yep ...
that's right ... but to mention: a more elegant and performant way to declare and init an array is the following way - i comment the code ... read it carefully :) -
/**
-
* declaring and initializing arrays
-
*/
-
-
// declaring the standard way out of every book :)
-
var list = new Array();
-
-
// put elements in it
-
list[0] = 'val0';
-
list[1] = 'val1';
-
-
// put it together in one step
-
var list = new Array('val0', 'val1');
-
-
// since we sometimes want to have an empty list (array)
-
// we simply declare one ... but! we dont need to use the
-
// brackets because js don't need to eval them when we
-
// don't put params in ... slightly better version is now:
-
var list = new Array;
-
-
// it is better to use literals ... only 2 chars and js don't need
-
// to interpret 2 keywords ;) so: best is to use literals when having
-
// them
-
var list = [];
-
-
// to init values you may use the literals too
-
var list = ['val0', 'val1'];
-
kind regards
ps: the same goes for objects :)
phvfl 173
Recognized Expert New Member
yep ...
that's right ... but to mention: a more elegant and performant way to declare and init an array is the following way - i comment the code ... read it carefully :) -
/**
-
* declaring and initializing arrays
-
*/
-
-
// declaring the standard way out of every book :)
-
var list = new Array();
-
-
// put elements in it
-
list[0] = 'val0';
-
list[1] = 'val1';
-
-
// put it together in one step
-
var list = new Array('val0', 'val1');
-
-
// since we sometimes want to have an empty list (array)
-
// we simply declare one ... but! we dont need to use the
-
// brackets because js don't need to eval them when we
-
// don't put params in ... slightly better version is now:
-
var list = new Array;
-
-
// it is better to use literals ... only 2 chars and js don't need
-
// to interpret 2 keywords ;) so: best is to use literals when having
-
// them
-
var list = [];
-
-
// to init values you may use the literals too
-
var list = ['val0', 'val1'];
-
kind regards
ps: the same goes for objects :)
You live and learn, cheers
gits 5,390
Recognized Expert Moderator Expert
You live and learn, cheers
;) yep ...all the time
but the things i mentioned are things that you encounter when making heavy use of javascript, due to building ajax-applications. sometimes you have huge amounts of data to process ... and than you search for optimizations on everything ;) ... when using js only the way most users do ... there is no explicit need to do or even to know such things ... but in case you DO know and use it that way you may ready for takeoff with more complex things ...
kind regards
You are declaring an new array on each line at the moment. To add multiple pages to the use: -
var pages = new Array();
-
pages[0]="page1.htm"
-
pages[1]="page2.htm"
-
pages[2]="page3.htm"
-
Just increment the index each time. The compare function code will need to be on each page that uses it, and will return true if a match is made.
If you continue to have problems please post the javascript that you are using so that I can check through it.
I have tried both your way and the way that gits mentioned however it still won't work
here is a copy of the code at it's current stage: - <!--
-
Array.prototype.contains=function($str){
-
var $blFound = new Boolean();
-
var $reg = new RegExp("^" + $str + "$", "i")
-
for (var i = 0;i<this.length;i++){
-
if(this[i].match($reg)){
-
$blFound=true;
-
}
-
}
-
return $blFound;
-
}
-
function validate(url){
-
outlink = url
-
var pages = new Array;
-
var pages = ['page1.htm', 'page2.htm'];
-
if (pages.contains(outlink)){
-
outlink = 'error.htm'
-
}
-
window.location.href=(outlink)
-
}
-
// -->
P.S. is this code server-side?
phvfl 173
Recognized Expert New Member
The code is javascript, so it is client side.
What is the value that is being passed in for the url variable? If there is trailing information before the filename e.g. url=location/page1.htm then it would not currently match. If this is the case then remove the "^" from the $reg declaration.
I have just been playing about with calling this. Even when the return from the contains function is false the code in the if loop was being executed. Change the if loop statement to: -
if (pages.contains(outlink)==true){
-
outlink = 'error.htm'
-
}
-
Taking into account what Gits posted the code can be made more efficient by using it as such. -
Array.prototype.contains=function($str){
-
//removed keywords for new boolean and set it to false directly.
-
var $blFound = false;
-
var $reg = new RegExp("^" + $str + "$", "i")
-
for (var i = 0;i<this.length;i++){
-
if(this[i].match($reg)){
-
$blFound=true;
-
}
-
}
-
return $blFound;
-
}
-
-
function validate(url){
-
outlink = url
-
-
//removed duplicate declaration of variable
-
var pages = ['page1.htm', 'page2.htm'];
-
alert(pages.contains(outlink));
-
if (pages.contains(outlink)==true){
-
outlink = 'error.htm'
-
}
-
window.location.href=(outlink)
-
}
-
The comments can be removed and have been included to show the changes.
The code is javascript, so it is client side.
What is the value that is being passed in for the url variable? If there is trailing information before the filename e.g. url=location/page1.htm then it would not currently match. If this is the case then remove the "^" from the $reg declaration.
I have just been playing about with calling this. Even when the return from the contains function is false the code in the if loop was being executed. Change the if loop statement to: -
if (pages.contains(outlink)==true){
-
outlink = 'error.htm'
-
}
-
Taking into account what Gits posted the code can be made more efficient by using it as such. -
Array.prototype.contains=function($str){
-
//removed keywords for new boolean and set it to false directly.
-
var $blFound = false;
-
var $reg = new RegExp("^" + $str + "$", "i")
-
for (var i = 0;i<this.length;i++){
-
if(this[i].match($reg)){
-
$blFound=true;
-
}
-
}
-
return $blFound;
-
}
-
-
function validate(url){
-
outlink = url
-
-
//removed duplicate declaration of variable
-
var pages = ['page1.htm', 'page2.htm'];
-
alert(pages.contains(outlink));
-
if (pages.contains(outlink)==true){
-
outlink = 'error.htm'
-
}
-
window.location.href=(outlink)
-
}
-
The comments can be removed and have been included to show the changes.
Thanks guys for all of your help it does work correctly now on all parts of my site
gits 5,390
Recognized Expert Moderator Expert
hi ...
glad to hear you got it to work :) ... post back to the forum anytime you have more questions ...
kind regards
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Kepler |
last post by:
How do you get the height of the client browser in IE? Both
document.body.clientHeight and document.body.offsetHeight return the
height of the document. If the page is long and there's a vertical...
|
by: Tom Szabo |
last post by:
Hi All,
What I want to achieve is to display a login screen in a certain size and
appearance window with no toolbars etc.
To achieve this I think I need to use redirection, but I am not sure...
|
by: Robert Oschler |
last post by:
I am working on a PHP 4 app that interacts with an external authorization
server. The external server does "third-party" authorization of users.
So I do the following:
1) Each of my PHP scripts...
|
by: AVance |
last post by:
Hi,
I've come across this scenario in ASP.NET 1.1 with forms authentication
where the forms auth doesn't seem to timeout correctly, nor redirect to the
login page.
I have done some testing, and...
|
by: deepukutty |
last post by:
HI all,
I am using IE(Internet Explorer) as my default browser for asp.net
application development.
Today i faced a strange problem. When ever an exception occured in the page
....application...
| |
by: WRH |
last post by:
Hello
I am new to asp but I made some Jscript functions which work
fine. The functions contain some strings used as a registration
key for some apps. It is important that these strings not be...
|
by: Flic |
last post by:
Hi,
I have a basic db that I access with MySQL query browser. Everything
seems fine to me but I am using this db as part of a php shopping
basket and when I try to add an item I get:
Notice:...
|
by: mansb2002 |
last post by:
Hi,
We recently moved our webserver from Win2K to Win2003. The application
works fine. Only problem is that when user views a report as a PDF, IE
does not show it. The following code is used to...
|
by: Jonathan Wood |
last post by:
I've spent days trying to come up with a solution. I'd appreciate it if
anyone can help.
My site requires all users to log on. There are three different roles of
users, and each user type will...
|
by: LayneMitch via WebmasterKB.com |
last post by:
I'm supposed to develop a page that asks info as form values and when you hit
"submit" it takes you to a page that reads the values you entered into the
first page and displays those values in a...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |