Wednesday, September 21, 2011

Validate URL in ASP.NET

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function Validate() {
var txtURL = document.getElementById("<%=txtURL.ClientID %>");
if (txtURL.value == "http://") {
ValidatorEnable(document.getElementById("<%=rxURL.ClientID %>"), false);
}
else {
ValidatorEnable(document.getElementById("<%=rxURL.ClientID %>"), true);
return document.getElementById("<%=rxURL.ClientID %>").isvalid;
}
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RegularExpressionValidator ID="rxURL" runat="server" ControlToValidate="txtURL" SetFocusOnError="true"
ValidationGroup="requiredGroup" Enabled="false" ErrorMessage="*" ValidationExpression="[http(s)?://]*([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"></asp:RegularExpressionValidator>
<asp:TextBox runat="server" ID="txtURL"></asp:TextBox>
<asp:Button ID="btnCheckURL" Text="Check URL" runat="server" OnClientClick="return Validate();" />
</div>
</form>
</body>
</html>

No comments: