Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii friends,

I want javascript for textbox that validate textbox value.
Textbox value should start with http:\\ or https:\\

I dont know javascript much..
plzz help me.
Posted

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery.js.js"></script>
    <script type="text/javascript">
        var validate = function (control) {
            var value = control.value;
            if (value.substring(0, 7) == "http:\\\\" || value.substring(0, 8) == "https:\\\\")
                return true;
            else
            { alert('invalid url'); return false; }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="txtUrl" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btn" runat="server" Text="submit" OnClientClick="return validate(txtUrl);" />
    </form>
</body>
</html>
 
Share this answer
 
Comments
Aysha Patel 11-Jan-14 7:50am    
thanks karthik
Karthik_Mahalingam 11-Jan-14 7:57am    
welcome aysha :)
In asp.net using jQuery and Javascript you can do it by following code

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" EnableViewState="false" %>

<!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>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
   
    <script type="text/javascript">
        if (typeof String.prototype.startsWith != 'function') {
            String.prototype.startsWith = function (prefix) {
                return this.slice(0, prefix.length) == prefix;
            };
        }
        function ValidateUrl() {          
            var url = $('#<%=txtUrl.ClientID %>').val();
            if (url.startsWith("http:\\") || url.startsWith("https:\\")) {
                alert(url);
                return true;
            }
            else {
                alert("it is not valid");
                return false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   <asp:TextBox ID="txtUrl" runat="server"></asp:TextBox>
     <asp:Button ID="btnSubmit" runat="server" Text="Submit" onClientClick="return ValidateUrl()" />
    </div>
   
    </form>
</body>
</html>
 
Share this answer
 
v2
Comments
Aysha Patel 11-Jan-14 5:25am    
.startwith method does not exist..
Sandeep Singh Shekhawat 11-Jan-14 5:50am    
I have updated above code and you can use this updated code. It will work on all browsers. Thanks
Aysha Patel 11-Jan-14 7:47am    
thanks sandeep

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900