I need to implement a url regular expression valilator in javascript. I am using the code that is not correctly validating (i.e when we enter ww.google.com it accepts and wwww.google.com also accepts) Plese help me to find the best code. My code is given
below; Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtValidURL" runat="server"></asp:TextBox>
<asp:Button ID="test" runat="server" Text="Button" OnClientClick="return validateURL();" />
<script type="text/javascript">
function validateURL() {
var txtValidURL = document.getElementById("<%=txtValidURL.ClientID %>").value;
var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
var t = 'www.google.com';
if (txtValidURL.match(regex) )
{
alert("Successful match");
Atiqiub
Member
54 Points
71 Posts
How we implement url regular expression valilator in javascript?
Feb 22, 2012 02:44 PM|LINK
Hi all,
I need to implement a url regular expression valilator in javascript. I am using the code that is not correctly validating (i.e when we enter ww.google.com it accepts and wwww.google.com also accepts) Plese help me to find the best code. My code is given below; Thanks.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test5.aspx.cs" Inherits="test5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtValidURL" runat="server"></asp:TextBox>
<asp:Button ID="test" runat="server" Text="Button" OnClientClick="return validateURL();" />
<script type="text/javascript">
function validateURL() {
var txtValidURL = document.getElementById("<%=txtValidURL.ClientID %>").value;
var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
var regex = new RegExp(expression);
var t = 'www.google.com';
if (txtValidURL.match(regex) )
{
alert("Successful match");
} else {
alert("No match");
}
return false;
}
</script>
</div>
</form>
</body>
</html>
Atiq.
nivvi
Member
68 Points
14 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 22, 2012 07:21 PM|LINK
Refer to below links:
http://lawrence.ecorp.net/inet/samples/regexp-parse.php
http://lawrence.ecorp.net/inet/samples/regexp-parse.php
http://www.456bereastreet.com/archive/201105/validate_url_syntax_with_javascript/
nivvi
Member
68 Points
14 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 22, 2012 07:22 PM|LINK
Refer to below link
http://stackoverflow.com/questions/1000327/validate-url-with-javascript-and-php
A1ien51
All-Star
29935 Points
5821 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 23, 2012 02:38 AM|LINK
technically ww.google.com is a valid url.
Eric
Danny Gokey
Member
290 Points
56 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 23, 2012 08:10 AM|LINK
Hi,
it seems that it is not mandatory to enter "www", Actually, it is optional, and many sites have additional subdomains, this site, for
instance,has"forums.asp.net",where it use "forums" instead of "www".
Smith Peter
Member
142 Points
52 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 23, 2012 12:19 PM|LINK
Hi,
This might help you:
Regular Expression Library
Javascript Regular Expression Validator | Regular Expressions Library and Testing Tool
Atiqiub
Member
54 Points
71 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 23, 2012 01:54 PM|LINK
Dear Danny Gokey, "forums.asp.net" is a sub domain so it should be written as it is
but main domain commonly starts with www.
Thanks for the reply 
Atiq
Atiqiub
Member
54 Points
71 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 23, 2012 01:58 PM|LINK
Thanks all, I will try your suggestions and will mark as answer :)
Atiq
Atiqiub
Member
54 Points
71 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 24, 2012 02:54 PM|LINK
Smith, Your link is very good but unfortunatly I can not got the right one
Thanks for reply.
Allen Li - M...
Star
10411 Points
1196 Posts
Re: How we implement url regular expression valilator in javascript?
Feb 28, 2012 01:46 AM|LINK
Hi, you can try the following regular expression:
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework