/    Sign up×
Community /Pin to ProfileBookmark

Help with URL validation – code doesn’t work

I’m very new to Javascript, so if I made any stupid mistakes, well, that’s why. ?

Here’s the function that doesn’t work, I need to validate the entry into the text box to make sure it’s a valid URL.

[code]
function valid() {
if (/^(http(s?)://|ftp://{1})((w+\.)+)w{2,}(/?)$/.test(urlbox.value)) }
alert(“DEBUG-working”);
javascript:changeFrame()
}
else {
alert(urlbox.value);
alert(“The website you entered was invalid.”);
field.focus();
field.select();
return false;
}
}
[/code]

Can anyone tell me what’s wrong with the above code? It’s probably something with the regular expression (I think that’s what you call it), but I don’t know what.
All I really need is for it to check to make sure the string starts with http:, I don’t need it to work with https/ftp.

Thanks!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@zachzachNov 02.2003 — try:
<i>
</i>function valid() {
if(urlbox.value.substring(0,indexOf(":")) == "ftp"||"http"||"https" &amp;&amp; urlbox.value.substring(indexOf(":")+1,indexOf(":")+3) == "\\"||"//" &amp;&amp; urlbox.value.substring(urlbox.value.length-4,urlbox.value.length) == "html"||".htm"||".asp"||"aspx"||"shtm"||"***/"||"***/") {
alert("DEBUG-working");
javascript:changeFrame()
}else {
alert(urlbox.value);
alert("The website you entered was invalid.");
field.focus();
field.select();
return false;
}
}


im not sure about the *'s, substitute them for whatever 'any charicter' is.

that allows:

(http|fpt|https)://********/*****/****/***(ect)/***(.htm|.html|.shtm|.shtml|.asp|.aspx|just /|just )

i hope you got that confusing thing i just wrote

zach

become a member of my forums please!
Copy linkTweet thisAlerts:
@pilot1authorNov 02.2003 — It doesn't work - nothing happens when I click the submit button, probably because of the wildcards.

I have no idea what the correct character to use for a wildcard is, though..

Here is the entire webpage, just in case something other than that Function is set up wrong.
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
&lt;head&gt;
&lt;title&gt;CHEIRON - FBS&lt;/title&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /&gt;

&lt;script language="javascript" type="text/javascript"&gt;
&lt;!--
function changeFrame() {
document.all.theframe.src = urlbox.value
}
function valid() {
if(urlbox.value.substring(0,indexOf(":")) == "ftp"||"http"||"https" &amp;&amp; urlbox.value.substring(indexOf(":")+1,indexOf(":")+3) == "\\"||"//" &amp;&amp; urlbox.value.substring(urlbox.value.length-4,urlbox.value.length) == "html"||".htm"||".asp"||"aspx"||"shtm"||"***/"||"***/") {
alert("DEBUG-working");
java script:changeFrame()
}else {
alert(urlbox.value);
alert("The website you entered was invalid.");
field.focus();
field.select();
return false;
}

// --&gt;
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;p&gt;Enter URL here:
&lt;input type="text" name="urlbox" value="http://www.google.com" /&gt;
&lt;input type="submit" name="Submit" value="Go!" onClick="javascript:valid()" /&gt;
&lt;/p&gt;
&lt;iframe name="theframe" width ="100%" height="90%" scrolling="yes" src="http://www.google.com"&gt;&lt;/iframe&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@zachzachNov 04.2003 — well its probably the indexof thing and my numbers ( the +1,+3,all that stuff) is wrong...im kinda intermediate so...if you acually understand the code, just try tweaking it over and over and over, try taking code that uses indexof and .substring and figuring out how it works(and if they dont enter a or a / then the wildcards are pointless it was just to fix that problem)

well hope you get it to work ill keep trying 2
Copy linkTweet thisAlerts:
@pilot1authorNov 04.2003 — I got it working with a Perl script, thanks for your help.
Copy linkTweet thisAlerts:
@zachzachNov 04.2003 — should have though of that lol

perl has like the best string editing functions(that i know about)
Copy linkTweet thisAlerts:
@RajinApr 20.2006 — Hi,

I am in the process writing a URL Validating function in javascript. Kindly help me out.

Thanks

Rajin
×

Success!

Help @pilot1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...