好了,我「借」從w3Schools.com這個代碼,所以我想給他們貸
<script type="text/javascript">
function val() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("spnFirstName").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "server.aspx", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send();
//xmlhttp.send("fname=Henry&lname=Ford"); //This is if you want to send values
}
</script>
First Name: <asp:TextBox ID="txtFirstName" runat="server" OnTextChanged="txtFirstName_TextChanged" AutoPostBack="True" onkeyup="Val()"></asp:TextBox>
<span id="spnFirstName" runat="server" style="color:Red"> </span>
Server.aspx是一個空頁只是在它裏面有「你好」這個詞。正如你所看到的,我將這個簡單的javascript函數綁定到了onKeyUp事件。這似乎也不妨礙ASP.NET AJAX驗證。
我喜歡做這個不是添加DLL好得多,因爲只要我堅持使用核心asp.net產品,我不必擔心有挖他們可能沒有圖書館其他開發商。
你究竟在做什麼驗證?表單域或客戶端的其他東西?或者你是否驗證服務器端的AJAX代碼傳遞的數據? 也只是好奇;你正在研究一個MVC應用程序或Web窗體? –
我想驗證在服務器端的文本框中輸入的內容。 – Limey