...

View Full Version : validating URL input. help!



etahchen
03-02-2009, 06:25 AM
ahh! please help me. i'm sure this is easy is you know javascript already. thanks.

this is supposed to validate an URL input. so it checks first for "http://" i put four of //// cause aren't they special characters that i have to escape? and it i check for letters and numbers. then it has to end with a "." and then at least three letters.

<html>
<head>
<SCRIPT language="JavaScript">

function check_it()
{
to_match=/http://[a-zA-Z0-9-].{1,3}/
var okay= document.s1.t1.value;
if (to_match.test(okay))
{
window.alert("okay great URL!");
return true;
}
else
{
window.alert("NO! try again!");
return false;
}

</SCRIPT>
</HEAD>
<body>
<FORM name="s1" onSubmit="return check_it()">
<INPUT type="text" name="t1">
<INPUT type="submit" action="#">
</FORM>
</body>
</HTML>

abduraooft
03-02-2009, 07:26 AM
Try
function check_it()
{
to_match=/^http:\/\/[a-zA-Z0-9-]*\.[a-zA-Z]{1,3}$/
var okay= document.s1.t1.value;
if (to_match.test(okay))
{
window.alert("okay great URL!");
return true;
}
else
{
window.alert("NO! try again!");
return false;
}
}

etahchen
03-02-2009, 07:33 PM
thanks for that help. it still didn't work. even if i put in a good url, the browser will still say "NO! Try again!".
but i'm closer to my goal now

abduraooft
03-03-2009, 07:19 AM
thanks for that help. it still didn't work. even if i put in a good url, the browser will still say "NO! Try again!".
but i'm closer to my goal now
Could you post some sample 'good urls'?

etahchen
03-03-2009, 07:44 AM
samples:

http://www.yahoo.com
http://www.ask.com

abduraooft
03-03-2009, 07:55 AM
Try

to_match=/^http:\/\/(www\.){0,1}[a-zA-Z0-9-]*\.[a-zA-Z]{1,3}$/

etahchen
03-04-2009, 02:02 AM
YYAAAYYY!!!!! thank you! now i'm going to study this and make sure i understand this thoroughly



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum