Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

URL Validation

Status
Not open for further replies.

pixel69

Technical User
Sep 25, 2002
56
ZA
How can i validate an input field to ensure that a URL has been entered correctly? ie:
Thanks,

pixel
 
Do you want to validate only HTTP protocol URLs, or allow for other protocols as well?

Also, do you really want to ensure that "www" is always entered if the protocol is HTTP? I ask, as this is NOT a requirement of the HTTP protocol - machines can be given any name, it's just that "www" has caught on for web server names.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I guess what I'm asking in a nutshell is:

Do you want to validate against a known spec (for URLs), or do you want to validate against your own requirements?

The two are clearly very different, if your above example is the only thing you want URLs to begin with.

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Here is an example test page that shows how you cuold use a regular expression to do this:
Code:
<html>
<head>
<title></title>
<script type="text/javascript">
function validateData(_myKey){
  var bValid = /^http:\/\/www\.([^.])+\.([^.])+$/.test(_myKey);
  return bValid;
}
</script>
</head>
<body>
<form>
<input name="mydata" value="[URL unfurl="true"]http://www.google.com">[/URL]
<a href="javascript:alert(validateData(document.forms[0].elements['mydata'].value));">Test</a>
</form>
</body>
</html>
This code requires all URLs to start: (and will have some text followed by a . and then more text. It's not a perfect solution [smile]

Cheers,
Jeff


[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Thanks guys for your feedback.
As you mention BillyRayPreachersSon, I would just like to validate a field if it contains a http:// & or ".". I only remebered now that there are lots of sites that don't have the
Thanks
Pixel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top