View Full Version : Input
FrEaKmAn
July 25th, 2006, 09:42 PM
I have simple input, you probably know I working on this site for links. Now there is already set to start your link with "http://" but some people just erase it or add link so then we get http://http://www... Now I would like alert to show up if somebody make this kind of mistake.
degsy
July 25th, 2006, 09:52 PM
You can do simple validation on strings
<script type="text/javascript">
function checkURL(){
str = document.form1.url.value;
if(str == 'http://' || str.indexOf('http://')== -1){
alert('Please enter a URL');
return false;
}
if(str.indexOf('http://http://')!= -1){
alert('Invalid URL: Please check the URL');
return false;
}
}
</script>
<form id="form1" name="form1" method="post" action="" onsubmit="return checkURL()">
<input type="text" name="url" id="url" />
<input type="submit" name="Submit" value="Submit" />
</form>
If you are submitting data then you should validate server side aswell as client side.
FrEaKmAn
July 25th, 2006, 10:07 PM
works great, thanks m8
degsy
July 26th, 2006, 11:01 AM
Just refined the script a bit
<script type="text/javascript">
function checkURL(){
str = document.form1.url.value;
if(str == 'http://'){
alert('Please enter a URL');
return false;
}
if(str.indexOf('http://http://')!= -1 || str.indexOf('http://')== -1){
// \n = newline
// \t = tab
msg = 'Invalid URL: The URL must be in the format' + '\n\n'
msg += 'Example:' + '\n\t';
msg += 'http://domain.com' + '\n\t';
msg += 'http://www.domain.com' + '\n\t';
msg += 'http://subdomain.domain.com';
alert(msg);
return false;
}
}
</script>
FrEaKmAn
July 26th, 2006, 09:10 PM
no problem, the first works fine. But what about making that it has to include domain name, .com . net ?? Maybe not to good idea because of the big list of domains, so no need. Thanks anyway
degsy
July 27th, 2006, 02:01 PM
Personally I wouldn't for the reason you gave.
If you were going to do it then setup an array of extensions and loop through them to find a match.