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.
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> |
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. |
|||
|
|||
1 comments :
Not working. I have tested with http://www.abc.com
Note: Only a member of this blog may post a comment.