PDA

View Full Version : Validate url



lay
07-22-2009, 08:15 AM
Hello everybody,

I want to get the javascript code that can validate url.

Can someone help me?

Thank you for your kind.

rainarts
07-28-2009, 08:52 AM
Hi,

In able to this, you will be needing some event handling inside your page, so that you will be able to trigger specific functions' stated or attached on those events.

Since we don't have any reference, then i will just have to simulate everything using an onload event with a simple prompt that i will provide on the user when they load the page.

Here's the code:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>http://www.dynamicdrive.com ™</title>
<script type="text/javascript">
<!--

onload = ( function() {
/* JavaScript Regular Expression DEMO ~
********************************
Valid URL sample :
********************************
- http://www.dynamicdrive.com/
- http://www.w3.org/
- http://www.google.com/

********************************
Non-Valid URL sample :
********************************
- www.dynamicdrive.com/
- http://dynamicdrive.com/
- http://www.dynamicdrive.com/subfolder/
- http://www.dynamicdrive.com
_______________________
~ The pattern i used on this demo will require the user to enter only the absolute path of the site, excluding any subfolder location.
*******************************/
var count;
var isUserInput;
var isHome = ( /^(?:http://)w{3}(?:\.)[a-z0-9_\-]+\.?(?=\w+)\w{3}\/$/ );
do {
isUserInput = prompt((( count ) ? "Invalid URL address!" : "Enter Address :" ), isUserInput );
count = (( isHome.test( isUserInput )) ? 0 : 1 );
} while( count !== 0 );
location.replace( isUserInput );

} );

// ECMA ( JavaScript v1.5 ) -->
</script>
</head>
<body>
<div id="main"></div>
</body>
</html>

hope it does get what you need...