用于文本框的Javascript [英] Javascript for textbox
本文介绍了用于文本框的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hii朋友,
我希望javascript用于验证文本框值的文本框。
文本框值应以http:\\\开头\\或https:\\
我不太了解javascript ..
plzz帮助我。
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.
推荐答案
<%@ 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>
在使用jQuery和Javascript的asp.net中,您可以通过以下代码
In asp.net using jQuery and Javascript you can do it by following code
<%@ 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( 它无效);
return false ;
}
}
< / script >
< / head >
< 正文 >
< < span class =code-leadattribute> form id = form1 runat = server >
< div >
< asp:TextBox ID = txtUrl runat = server > < / asp:TextBox >
< asp:按钮 ID = btnSubmit runat = server文本 = 提交 onClientClick = 返回ValidateUrl() / >
< / div >
< / form >
< / body >
< < span class =code-leadattribute> / html >
('#<%=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>
这篇关于用于文本框的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文