Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Introduction:

Here I will explain how to validate the textbox and whether that textbox contain valid email or url address using asp.net

Description:

Here I will explain how to validate the textbox and whether that textbox contain valid email or url address using asp.net.

First write the following code in aspx page 



html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function IsValidUrl() {
var textbox = document.getElementById('<%=txt1.ClientID %>');
//Regular Expression for Url validation.
var url = /^http[s]*\:\/\/[wwW]{3}\.+[a-zA-Z0-9]+\.[a-zA-Z]{2,3}.*$|^http[s]*\:\/\/[^w]{3}[a-zA-Z0-9]+\.[a-zA-Z]{2,3}.*$|http[s]*\:\/\/[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}.*$/;
var email = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
//Checks with the control value.
if (textbox.value.length > 0) {
if ((url.test(textbox.value))||(email.test(textbox.value)))
{
return true;
}
else {
//If the condition not satisfied shows error message.
alert("Please enter valid URL or Email");
return false;
}
}
else {
alert("please enter text");
return false;
}
}//End of function IsValidUrl.
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt1" runat="server"/>
<asp:Label ID="lbltext" runat="server"> <span style= "color:Red"> URL like http://www.aaa.com </span></asp:Label>
<br />
<asp:Button ID="btnsubmit" Text="Submit" runat="server" OnClientClick="javascript:IsValidUrl();" />
</div>
</form>
</body>
</html>
Now run the page you will get validation for textbox

For more JavaScript validations check these links

http://www.aspdotnet-suresh.com/2010/10/javascript-validations-for-emailphone.html

http://www.aspdotnet-suresh.com/2010/10/javascript-validations-to-validate-form.html

I hope it helps you

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Mubashir Ahmed Shaikh said...

Not working. I have tested with http://www.abc.com

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.