473,569 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validate URL script help

MJ
For some reason the following script does not work in Netscape/Mozilla, but
works fine in IE and Opera. It is supposed to check the syntax, make sure
there is a valid TLD (yes, those are all of the current TLDs), and allow for
addresses with or without trailing slashes or page addresses.

Anybody have any ideas on how to get this to work in Netscape? I suspect it
has something to do with the regular expression, but I can't get it to work.
Any help would be GREALY appreciated!

// Validate URL
re3 =
/^(http|https):\/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at
|au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c
|cd|cf|cg|ch|ci |ck|cl|cm|cn|co |com|coop|cr|cu |cv|cx|cy|cz|de |dj|dk|dm|do|dz |
ec|edu|ee|eg|er |es|et|fi|fj|fk |fm|fo|fr|ga|gb |gd|ge|gf|gg|gh |gi|gl|gm|gn|go v
|gp|gq|gr|gs|gt |gu|gw|gy|hk|hm |hn|hr|ht|hu|id |ie|il|im|in|in fo|int|io|iq|ir |
is|it|je|jm|jo| jp|ke|kg|kh|ki| km|kn|kr|kw|ky| kz|la|lb|lc|li| lk|lr|ls|lt|lu| l
v|ly|ma|mc|md|m g|mh|mil|mk|ml| mm|mn|mo|mp|mq| mr|ms|mt|mu|mus eum|mv|mw|mx|my |
mz|na|name|nc|n e|net|nf|ng|ni| nl|no|np|nr|nu| nz|om|org|pa|pe |pf|pg|ph|pk|pl |
pm|pn|pr|pro|ps |pt|pw|py|qa|re |ro|ru|rw|sa|sb |sc|sd|se|sg|sh |si|sj|sk|sl|sm |
sn|so|sr|st|su| sv|sy|sz|tc|td| tf|tg|th|tj|tk| tm|tn|to|tp|tr| tt|tv|tw|tz|ua| u
g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)([/]\S+|)$/i;

function validateURL(tex tfield){
if (textfield.valu e == ""){
return true;
} else {
if (textfield.valu e.substring(0,7 ) != "http://" &&
textfield.value .substring(0,8) != "https://") {
textfield.value = "http://" + textfield.value ;
}
if (!re3.test(text field.value)){
alert("Invalid web site address");
textfield.focus ();
}
return false;
}
}

It is being called by a simple:

<input name="Website" type="text" onBlur="validat eURL(this)">
Jul 23 '05 #1
3 3368
"MJ" <no*****@thank. you> writes:
For some reason the following script does not work in Netscape/Mozilla, but
works fine in IE and Opera.
"does not work" how? Do you get an error message or does it accept the wrong
strings?
/^(http|https):\/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at
|au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c
Your newsclient has wrapped the line. It should be on one line to work.
g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)([/]\S+|)$/i;

^
That slash should be escaped. Change "[/]" to "\/".

Not tested (didn't want to rewrap the lines :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
MJ
Ah, it was the regular expression. Escaping the slash fixed it. I could
have sworn I had tried that before, but I guess not.

Thanks for the help! You're a life saver.
"Lasse Reichstein Nielsen" <lr*@hotpop.com > wrote in message
news:ll******** **@hotpop.com.. .
"MJ" <no*****@thank. you> writes:
For some reason the following script does not work in Netscape/Mozilla, but works fine in IE and Opera.
"does not work" how? Do you get an error message or does it accept the

wrong strings?
/^(http|https):\/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at
|au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c
Your newsclient has wrapped the line. It should be on one line to work.
g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)([/]\S+|)$/i; ^
That slash should be escaped. Change "[/]" to "\/".

Not tested (didn't want to rewrap the lines :)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 23 '05 #3
MJ wrote:
// Validate URL re3 = /^(http|https):
The alternation can be written as /https?/ which is generally
more efficient ("http" can be matched always only once).
\/\/\S+\.(ac|ad|ae| aero|af|ag|ai|a l|am|an|ao|aq|a r|arpa|as|at
|au|aw|az|ba|bb |bd|be|bf|bg|bh |bi|biz|bj|bm|b n|bo|br|bs|bt|b v|bw|by|bz|ca|c c
|cd|cf|cg|ch|ci |ck|cl|cm|cn|co |com|coop|cr|cu |cv|cx|cy|cz|de |dj|dk|dm|do|dz |
ec|edu|ee|eg|er |es|et|fi|fj|fk |fm|fo|fr|ga|gb |gd|ge|gf|gg|gh |gi|gl|gm|gn|go v
|gp|gq|gr|gs|gt |gu|gw|gy|hk|hm |hn|hr|ht|hu|id |ie|il|im|in|in fo|int|io|iq|ir |
is|it|je|jm|jo| jp|ke|kg|kh|ki| km|kn|kr|kw|ky| kz|la|lb|lc|li| lk|lr|ls|lt|lu| l
v|ly|ma|mc|md|m g|mh|mil|mk|ml| mm|mn|mo|mp|mq| mr|ms|mt|mu|mus eum|mv|mw|mx|my |
mz|na|name|nc|n e|net|nf|ng|ni| nl|no|np|nr|nu| nz|om|org|pa|pe |pf|pg|ph|pk|pl |
pm|pn|pr|pro|ps |pt|pw|py|qa|re |ro|ru|rw|sa|sb |sc|sd|se|sg|sh |si|sj|sk|sl|sm |
sn|so|sr|st|su| sv|sy|sz|tc|td| tf|tg|th|tj|tk| tm|tn|to|tp|tr| tt|tv|tw|tz|ua| u
g|uk|um|us|uy|u z|va|vc|ve|vg|v i|vn|vu|wf|ws|y e|yt|yu|za|zm|z w)
That can be shortened very much to

\/\/\S+\.(aero|arpa |a[c-gilm-oq-uwz]|biz|b[a-bd-fjm-or-tvwyz]|com|coop
|c[acdf-ik-oruvx-z|d[ejkmoz]|edu|e[cegr-t]|f[i-kmor]|gov|g[abd-il-npr-uwy]
|h[kmnrtu]|info|int|i[del-oqr-t]|j[emop]|k[eghimnrwyz]|l[abcikr-vy]
|museum|mil|m[acdghkl-z]|name|net|n[acefgilopruz]|org|om|pro|p[aefghk-nr-twy]
|qa|r[eouw]|s[a-eg-ort-vyz]|t[cdf-hjkm-prtvwz]|u[agkmsyz]|v[aceginu]
|w[fs]|y[etu]|z[amw])

(if I have not missed a character or two, but I think you get the
idea). That is not only shorter but can be more efficient than
complete alternation, depending on the type of RegExp engine used.
With a NFA, character classes are much more efficient than alternation
because matching can be done in parallel and thus much faster. (See
<http://www.oreilly.com/catalog/regex/chapter/ch04.html>, "Character
Classes vs. Alternation".) For ECMAScript and implementations , a NFA
is clearly involved in the matching process, if not the only engine
type used, as backreferences and capturing parantheses are supported.
So it is clearly a Good Thing to replace alternation with character
classes here and avoid alternation where possible.

But:

Are you sure you need the top level domain this precise while
the rest is checked rather sloppy? Are you prepared to maintain
that script as top level domains evolve? Why don't you allow
IPv4 addresses in URLs? They can be static. Why don't you stick
(close) to RFC 2396? The BNF grammar can be easily implemented
as a RegExp.
([/]\S+|)$/i;


As Lasse already pointed out, in RegExp literals every forward slash
must be escaped, even in character classes. In fact, the character
class does not help here. But you probably meant /((\/\S+)?)$/i.
Rule of thumb: Do not use alternation when not necessary, see above.
PointedEars

P.S.
Please take heed of Usenet/Internet standards and use an existing "From:"
address. Avoiding spam is no excuse for breaking standards and thus helping
to destroy the functionality of the involved media:
<http://www.interhack.n et/pubs/munging-harmful/>
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1632
by: Mark Griffin | last post by:
Hi... I am trying to convert a simple FAQ script to access mysql db rather than the Access db it was designed for. The originators of the script offer no support at all. I have managed to set up the mysql db and have frigged the script to get it running, however certain bits are not functional, i.e. the links on the categories.
6
1611
by: Brian | last post by:
Help.. We cannot get this script to work..can someone take a look at it and make suggestions? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Convert Temperature</title>
2
1314
by: Sev | last post by:
Website: www.flywessex.blogspot.com You can see from the page source what an effin' mess I'm making of the site. Aside from all the obvious alignment issues, I can't get the code to link to the web pages within the pop up boxes. They did a minute ago, and now I've been editing the template, they don't. Sorry if I have not supplied...
2
1447
by: . . | last post by:
Hi I need some help with java script . I have four link button on my ASPX page . Each button gives user specific message when they click on it .The first button is is the default button and the button text is already bold and rest of them is dim . When user click the second button that button text will be bold rest of them will be dim and...
5
1416
by: hoozdiss | last post by:
i installed apache 2.2 on winxp with php5. when i write a piece of html and include a php or java script in the html i have no problem. when i file the script and use something like <script src="script.php"></script> i get nothing. he finds the file - if i rename it - he complains. i'm guessing a config param. please help.
0
5436
by: Jay Douglas | last post by:
Hello all, I'm in need of a custom validator that: 1). Runs server side code using ajax through a client validate function 2). Client validate function does not set args.IsValid until the server responds 3). Any submit actions (via link button) do not take place until all client validate functions run.
1
1904
by: rmsterling | last post by:
All, Subject : SQL Server 2005 SSIS Script Help - XML Secure Pull in to DB table I was wondering if any of you could help me with something..... I want to design a SSIS script that will pull XML data from a secure HTTP website direct in to a local database table. I have had no training on SQL Server 2005 SSIS. I have a reasonable
1
1317
by: wesselinbg | last post by:
Hello everyone! My name is Veselin and i'm php and mysql coder. But now i have big problems with one javascript that i want to create (maybe because i don't understand nothing from javascript :)). I want to create validate form that search for illegal chars. "." "/" "_" "\" "," My form name: MyJavaScriptForm Field that i want to check: MyField...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.