Ajax即时实现服务端数据验证
1. 页面 *.aspx 前台代码如下:
1<body>
2 <form id="form1" runat="server">
3 <script type="text/javascript">
4 var cbo = new CallBackObject();
5 cbo.OnComplete=Cbo_Complete;
6 cbo.OnError=Cbo_Error;
7 function CheckUserName(UserName)
8 {
9 var msg = document.getElementById('lblMessage');
10 if( UserName.length > 0 )
11 {
12 cbo.DoCallBack('tbUsername','');
13 }
14 else
15 {
16 cbo.AbortCallBack();
17 msg.innerHTML = '';
18 }
19 }
20 function Cbo_Complete(responseText, responseXML)
21 {
22 var msg = document.getElementById('lblMessage');
23 if( responseText == '0' )
24 {
25 msg.innerHTML = '合法用户!';
26 msg.style.color='green';
27 }
28 else if( responseText == '1' )
29 {
30 msg.innerHTML = '用户名长度必须在 3 到 15 之间, 且不包含字母/数字/下划线以外的字符!';
31 msg.style.color = 'red';
32 }
33 else
34 {
35 msg.innerHTML = '用户名不存在!';
36 msg.style.color = 'red';
37 }
38 }
39 function Cbo_Error( status, statusText, responseText )
40 {
41 //alert( 'status=' + status + ' responseText=' + responseText + ' statusText=' + statusText );
42 }