java script for url validation | Newbie | | Join Date: Sep 2006
Posts: 1
| | |
I simply want the validation for website url checking.
But it should not start from http:// (example http://www.yahoo.com)
it should start from ww.(example www.yahhoo.com)
Thanx in advance
| | Newbie | | Join Date: Jul 2007
Posts: 2
| | | re: java script for url validation Quote:
Originally Posted by khushwinder I simply want the validation for website url checking.
But it should not start from http:// (example http://www.yahoo.com)
it should start from ww.(example www.yahhoo.com)
Thanx in advance plese set fields - if(document.frmRegister.WebSite.value!="")
-
{
-
if (document.frmRegister.WebSite.value.indexOf ('www', 0) == -1 || document.frmRegister.WebSite.value.length < 10)
-
{
-
if(document.frmRegister.WebSite.value.indexOf ('.com', 0) == -1 || document.frmRegister.WebSite.value.length < 10)
-
{
-
alert ("Enter a valid URL")
-
document.frmRegister.WebSite.focus();
-
return false;
-
}
-
}
-
}
| | Newbie | | Join Date: Jul 2007
Posts: 2
| | | re: java script for url validation - if(document.frmRegister.WebSite.value!="")
-
{
-
if (document.frmRegister.WebSite.value.indexOf ('www', 0) == -1 || document.frmRegister.WebSite.value.length < 10)
-
{
-
if(document.frmRegister.WebSite.value.indexOf ('.com', 0) == -1 || document.frmRegister.WebSite.value.length < 10)
-
{
-
alert ("Enter a valid URL")
-
document.frmRegister.WebSite.focus();
-
return false;
-
}
-
}
-
}
| | Newbie | | Join Date: Oct 2007
Posts: 4
| | | re: java script for url validation
Hi i m new in this site.
Plz help me out, i m stuck in website validation bcoz,
i want validation in this manner,
user can only enter the website in (http://www.test.com) or either (www.test.com)
accept this type of entry it shows alert that "plz enter the website in proper manner" ,
i used ur style but it gives a problem when we enter dot(.) in multiple times like
(http://wwwwww...........test.........com) or (wwwwww..........test..........com)
|  | Newbie | | Join Date: Mar 2008
Posts: 20
| | | re: java script for url validation
hi,
this may help you... -
function isURL ( txtId ){
-
-
var string = document.getElementById(txtId).value;
-
-
if ( string.search(/^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$/) != -1 ){
-
alert('Valid URL');
-
return true;
-
}
-
-
-
if ( string.search(/^[http://]+[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$/) != -1 ){
-
alert('valid URL');
-
return true;
-
}
-
-
alert('Not a valid URL');
-
document.getElementById(txtId).select();
-
document.getElementById(txtId).focus();
-
return false;
-
-
}
-
-
regards,
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: java script for url validation
Not quite. That would match some invalid ones and not match a lot of valid ones.
However, you've got the right idea with using regular expressions.
Which one you use depends on how accurate you wish to be. You can find some good ones by searching for "url regular expression".
| | Newbie | | Join Date: Oct 2007
Posts: 4
| | | re: java script for url validation
i had used regular expression but it did not check dot, that how many dot u have in between extensions that's y i m asking for the validation ....
i had used this validation, it check for http:// an www, but again i stuck for dots....
<!-- - if( (website != "") || (website != null) )
-
{
-
if (website.indexOf ('http://', 0) == -1 )
-
{
-
if (website.indexOf ('www.', 0) == -1 || website.length < 10)
-
{
-
if(website.indexOf ('.com', 0) == -1 || website.length < 10)
-
{
-
alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
-
document.filmsForm.url.focus();
-
document.filmsForm.url.value = "" ;
-
return false;
-
}
-
}
-
-
var split = website.split("\.");
-
-
if(isNaN(split[0]))
-
{
-
if( (split[0].length > 3) || (split[0].length < 2) )
-
{
-
alert("The Length Of www Must Be '3' !");
-
document.filmsForm.url.focus();
-
document.filmsForm.url.value = "" ;
-
return false;
-
-
}
-
}else
-
{
-
alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
-
document.filmsForm.url.focus();
-
document.filmsForm.url.value = "" ;
-
return false;
-
}
-
-
-
} else {
-
-
if (website.indexOf('www.', 0) == -1 || website.length < 10)
-
{
-
if(website.indexOf('.com', 0) == -1 || website.length < 10)
-
{
-
alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
-
document.filmsForm.url.focus();
-
document.filmsForm.url.value = "" ;
-
return false;
-
}
-
}
-
-
var splitfromhttp = website.split("http://");
-
-
var splitfromdot = splitfromhttp[1].split("\.");
-
-
if(isNaN(splitfromdot[0]))
-
{
-
if( (splitfromdot[0].length > 3) || (splitfromdot[0].length < 2) )
-
{
-
alert("The Length Of www Must Be '3' !");
-
document.filmsForm.url.focus();
-
document.filmsForm.url.value = "" ;
-
return false;
-
-
}
-
}else
-
{
-
alert ("Enter a Website name Like 'http://www.test.com' Or 'www.test.com' !")
-
document.filmsForm.url.focus();
-
document.filmsForm.url.value = "" ;
-
return false;
-
}
-
-
}
-
}
-
-->
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: java script for url validation
Rather than make so many checks, just use one regular expression, e.g.: - /^(http://)?www\.[a-z]+\.com$/gi
A very simple one which would only match letter URLs, not ones with numbers, hyphens, etc. It would also only match .com which is what you seem to be checking for.
| | Newbie | | Join Date: Oct 2007
Posts: 4
| | | re: java script for url validation Quote:
Originally Posted by acoder Rather than make so many checks, just use one regular expression, e.g.: - /^(http://)?www\.[a-z]+\.com$/gi
A very simple one which would only match letter URLs, not ones with numbers, hyphens, etc. It would also only match .com which is what you seem to be checking for.
Hey i test this regular expression on a software (RegesxBilder) but it shows that the regular expression is worng.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: java script for url validation
That was a quick example I came up with without testing. You'll need to escape the forward slashes: - /^(http:\/\/)?www\.[a-z]+\.com$/gi
Note that this is by no means a complete test. For that, you will need something far more complex.
| | Newbie | | Join Date: Oct 2007
Posts: 4
| | | re: java script for url validation Quote:
Originally Posted by acoder That was a quick example I came up with without testing. You'll need to escape the forward slashes: - /^(http:\/\/)?www\.[a-z]+\.com$/gi
Note that this is by no means a complete test. For that, you will need something far more complex.
Thanx I got my Solution through regular Expression ....Yeah! u r right its(regular expression) too easy to validate the url . And for other validation too. thanx a lot
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: java script for url validation
You're welcome. Glad it's working!
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|