Click to See Complete Forum and Search --> : Help with URL validation - code doesn't work


pilot1
11-01-2003, 05:47 PM
I'm very new to Javascript, so if I made any stupid mistakes, well, that's why. :D

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.



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;
}
}

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!

zachzach
11-01-2003, 08:27 PM
try:

function valid() {
if(urlbox.value.substring(0,indexOf(":")) == "ftp"||"http"||"https" && urlbox.value.substring(indexOf(":")+1,indexOf(":")+3) == "\\\\"||"//" && 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!

pilot1
11-01-2003, 08:43 PM
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.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>CHEIRON - FBS</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

<script language="javascript" type="text/javascript">
<!--
function changeFrame() {
document.all.theframe.src = urlbox.value
}
function valid() {
if(urlbox.value.substring(0,indexOf(":")) == "ftp"||"http"||"https" && urlbox.value.substring(indexOf(":")+1,indexOf(":")+3) == "\\\\"||"//" && 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;
}

// -->
</script>
</head>

<body>
<p>Enter URL here:
<input type="text" name="urlbox" value="http://www.google.com" />
<input type="submit" name="Submit" value="Go!" onClick="javascript:valid()" />
</p>
<iframe name="theframe" width ="100%" height="90%" scrolling="yes" src="http://www.google.com"></iframe>
</body>
</html>

zachzach
11-04-2003, 01:42 PM
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

pilot1
11-04-2003, 03:53 PM
I got it working with a Perl script, thanks for your help.

zachzach
11-04-2003, 04:06 PM
should have though of that lol
perl has like the best string editing functions(that i know about)