-------------------------------ASP.NET Ajax表单验证-HTML代码-----------------------------------------
.aspx中HTML代码:
<%@ Page language="c#" Codebehind="biaodanyan.aspx.cs" AutoEventWireup="false" Inherits="ajaxselect.biaodanyan" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Ajax验证表单</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<link href="StyleSheet1.css" rel="stylesheet">
<script type="text/ecmascript">
/********************************************************************************* 函数名称:resultRemain?* 功能说明:根据传递值,改变表单提示状态 * 参??? 数:id:表单span ID号;str:提示文字;CssClassName:改变提示颜色的CSS Style Class名称*********************************************************************************/
function resultRemain(id,stype,str,CssClassName) {
switch (stype) {
//默认
case "normal" : eval("span_"+id).className=CssClassName; break;
//正确
case "right" : eval("span_"+id).innerHTML=str; eval("error_"+id).innerHTML=""; eval("span_"+id).className=CssClassName; break;
//错误
case "error": eval("span_"+id).className=CssClassName; eval("error_"+id).innerHTML=str; eval("span_"+id).innerHTML=""; break;
//获得焦点
case "focus": eval("span_"+id).innerHTML=str; eval("span_"+id).className=CssClassName; default : break; } }
/******************************************************************************** * 函数名称:onFocusForm * 功能说明:密码输入框获得焦点时显示提示状态 * 参 数:o:表单对象 * 调用方式:表单控件名称.Attributes["onblur"] = "return onFocusForm(this);"; //(Asp.net) *********************************************************************************/
function onFocusForm(o,str) {
if(o){ resultRemain(o.id,"focus",str,"srchtab01"); } }
/******************************************************************************** * 函数名称:onCheckPwd * 功能说明:当表单失去焦点时,触发相应的密码检测 * 参 数:o:表单对象 * 调用方式:表单名称.Attributes["onblur"] = "return onCheckPwd(this);"; //(Asp.net) *********************************************************************************/
function onCheckPwd(o){
var p = o.value;
if(p){ //测试密码长度,包括汉字密码
var len = 0;
var ary = escape(p).match(/(%u[0123456789ABCDEF]{4})+/g);
if(ary){
len = unescape(ary.join("")).length * 2;
p = unescape(escape(str).replace(/(%u[0123456789ABCDEF]{4})+/g,""));
len += p.length; }else{ len = p.length; }
if(len < 6 || len > 20){
resultRemain(o.id,"error"," 密码有误 ","srchtab01");
return false; }else{ resultRemain(o.id,"right","填写正确 ","formnormal");
return true; } }else{ resultRemain(o.id,"error"," 密码不能为空 ","srchtab01");
return false; } }
/******************************************************************************** * 函数名称:onComparePwd * 功能说明:当表单失去焦点时,检测确认密码是否与密码相同 * 参数:o:表单对象 * 调用方式:表单名称.Attributes["onblur"] = "return onComparePwd(this1,this2);"; //(Asp.net) *********************************************************************************/
function onComparePwd(o1,o2){
var p = o1.value;
var q = o2.value;
if(p){ if(q!=p){ resultRemain(o1.id,"error"," 确认密码有误 ","srchtab01");
return false; }
else{
resultRemain(o1.id,"right","填写正确 ","formnormal");
return true; } }
else{ resultRemain(o1.id,"error"," 密码不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:strlen * 功能说明:检测字符串长度 * 参数:o:表单对象;len:字符串长度 * 调用方式:表单名称.Attributes["onblur"] = "return strlen(this,30);"; //(Asp.net) ***********************************************************************************/
function strlen(o,len) {
var p = o.value;
if(p) {
var l=p.length; i
f(l>len) {
resultRemain(o.id,"error"," 字符超过长度 ","srchtab01");
return false; }
else resultRemain(o.id,"right","填写正确 ","formnormal");
return true; }
else { resultRemain(o.id,"error"," 资料不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:onCheckPostCode(o) * 功能说明:检测邮编是否合法 * 参数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return onCheckPostCode(this);"; //(Asp.net) ***********************************************************************************/
function onCheckPostCode(o){
var str = o.value;
if(str){ var pattern = /^\d{6}$/;
if(!pattern.test(str)) {
resultRemain(o.id,"error"," 邮编输入不合法 ","srchtab01");
return false; }
else{ resultRemain(o.id,"right","填写正确 ","formnormal");
return true; } }else{ resultRemain(o.id,"error"," 邮编不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:onCheckTel(o) * 功能说明:检测电话号码是否合法或传真 * 参 数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return onCheckPostCode(this);"; //(Asp.net) ***********************************************************************************/
function onCheckTel(o){
var str = o.value;
if(str){ var pattern = /^((\d{3,4}-?\d{7,8})|(\d{7,8}))$/;
if(!pattern.test(str)) {
resultRemain(o.id,"error"," 号码格式不合法 ","srchtab01");
return false; }
else{ resultRemain(o.id,"right","填写正确 ","formnormal");
return true; } }
else{ resultRemain(o.id,"error"," 号码不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:onCheckMobile(o) * 功能说明:检测手机号码是否合法 * 参数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return onCheckMobile(this);"; //(Asp.net) ***********************************************************************************/
function onCheckMobile(o){
var str = o.value;
if(str){ var pattern = /^(0?13\d{9})$/;
if(!pattern.test(str)) {
resultRemain(o.id,"error"," 手机号码输入不合法 ","srchtab01");
return false; }
else{
resultRemain(o.id,"right","填写正确 ","formnormal");
return true; } }else{ resultRemain(o.id,"error"," 手机号码不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:onCheckEmail(o) * 功能说明:检测E-mail是否合法 * 参数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return onCheckEmail(this);"; //(Asp.net) ***********************************************************************************/
function onCheckEmail(o){
var em = o.value;
if(em){
var pattern = /^(\w|_|\.)+@((\w|_|\-)+\.)+(\w){2,3}$/;
if(!pattern.test(em)) {
resultRemain(o.id,"error"," E-mail输入不合法 ","srchtab01");
return false; }
else
{ resultRemain(o.id,"right","填写正确 ","formnormal");
return true; } }
else{ resultRemain(o.id,"error"," E-mail不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:ipcheck(o) * 功能说明:检测IP地址是否合法 * 参数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return ipcheck(this);"; //(Asp.net) ***********************************************************************************/
function ipcheck(o){
var ipstr=o.value;
if(ipstr){ var reg = /^((\d{1,3})(\.\d{1,3}){3})$/;
if(reg.test(ipstr)) {
var ary = ipstr.split('.');
for(key in ary) {
f(parseInt(ary[key]) > 255) {
resultRemain(o.id,"error"," IP输入不合法 ","srchtab01");
return false; } }
resultRemain(o.id,"right","填写正确 ","formnormal");
return true; } else { resultRemain(o.id,"error"," IP输入不合法 ","srchtab01"); return false; } }
else {
resultRemain(o.id,"error"," IP不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:checkNum(o) * 功能说明:检测是否为纯数字 * 参数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return checkNum(this);"; //(Asp.net) ***********************************************************************************/
function checkNum(o) {
var str=o.value;
if(str) {
var reg =/\D/;
if(!reg.test(str)) { resultRemain(o.id,"right","填写正确 ","formnormal");
return true; }
else
{ resultRemain(o.id,"error"," 起始标识应为纯数字! ","srchtab01");
return false; } }
else
{ resultRemain(o.id,"error"," 起始标识不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称:checkchinese(o) * 功能说明:检测是否为中文字符 * 参数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return checkchinese(this);"; //(Asp.net) ***********************************************************************************/
function checkchinese(o) {
var str=o.value; if(str) {
var reg =/[\u0391-\uFFE5]/gi; if(!reg.test(str)) {
resultRemain(o.id,"right","填写正确 ","formnormal");
return true; }
else
{ resultRemain(o.id,"error"," 含有中文字符! ","srchtab01");
return false; } }
else { resultRemain(o.id,"error"," 不能为空 ","srchtab01");
return false; } }
/********************************************************************************** * 函数名称: CheckUid(o) * 功能说明:检测用户名是否合法,暂没做ajax检测 * 参 数:o:表单对象; * 调用方式:表单名称.Attributes["onblur"] = "return CheckUid(this);"; //(Asp.net) ***********************************************************************************/
function CheckUid(o){
var u = o.value;
if(u){
if(u.length < 4 || u.length > 20) {
resultRemain(o.id,"error"," 用户名长度需在4-20之间! ","srchtab01");
return false; } }
else{ resultRemain(o.id,"error"," 用户名不能为空! ","srchtab01");
return false; }
var pattern = /^([a-zA-Z0-9]|[\u4E00-\uFA2D])+$/;
if(!pattern.test(u)) { resultRemain(o.id,"error"," 用户名包含非法字符! ","srchtab01");
return false; }
else
{ resultRemain(o.id,"right","填写正确 ","formnormal");
return true; }
// if(u == o1.value && u != ""){
// RegStyle(o.id,"right","此用户名可以使用");
// return false;
// }
// var flag = LoadXml(HostName + "/CallPage/Query.aspx?op=QueryUid&s=" + o.value);
// if(flag == 'False'){
// RegStyle(o.id,"right","此用户名可以使用");
// o1.value = u;
// }else{
// RegStyle(o.id,"error","这个用户名已被注册");
// return false;
// } } function RegForm() { var falsestr="";
//检测密码 if(!onCheckPwd(Form1.tb_u_password)) falsestr ="false"; if(!onCheckPwd(Form1.tb_u_password2)) falsestr ="false"; if(!onCheckEmail(Form1.tb_email)) falsestr ="false"; if(!onCheckTel(Form1.tb_tel)) falsestr ="false";
// if(!onCheckMobile(Form1.tb_mobile))
//falsestr +="false|"; if(!CheckUid(Form1.tb_name)) falsestr ="false";
//当falsestr中,包含false中,则返回false if(falsestr=="false") { alert('表单中,有不符合规定的字符'); return false; } else { return true; } }
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" onsubmit="return RegForm();">
<table cellSpacing="1" cellPadding="0" width="770" align="center" border="0">
<tr>
<td style="HEIGHT: 23px"><FONT face="宋体">用户名:</FONT></td>
<TD style="HEIGHT: 23px"><INPUT id="tb_name" type="text" runat="server" NAME="tb_name"></td>
<td><span id="span_tb_name" runat="server"></span><pan id="error_tb_name" runat="server"></span></td>
</tr>
<tr>
<td width=30%><FONT face="宋体">密码:</FONT></TD>
<td width=40%><INPUT id="tb_u_password" type="password" runat="server" NAME="tb_u_password"></td>
<td width=30%><span id="span_tb_u_password" runat="server"></span><span id="error_tb_u_password" runat="server"></span></td>
</tr>
<tr>
<td width=30%><FONT face="宋体">确认密码 :</FONT></td>
<td width=40%><INPUT id="tb_u_password2" type="password" runat="server"></td>
<td width=30%><span id="span_tb_u_password2" runat="server"></span><span id="error_tb_u_password2" runat="server"></span></td>
</tr>
<tr>
<TD width=30%><FONT face="宋体">e_Mail :</font></td>
<tdwidth=40%><INPUT id="tb_email" type="text" runat="server" NAME="tb_email"></td>
<td width=30%><span id="span_tb_email" runat="server"></span><span id="error_tb_email" runat="server"></span></td>
</tr>
<tr>
<TD width=30%> <FONT face="宋体">联系电话:</font></td>
<td width=40%><INPUT id="tb_tel" type="text" runat="server" NAME="tb_tel"></td>
<td wid<th=30%><span id="span_tb_tel" runat="server"></span><span id="error_tb_tel" runat="server"></span></td>
</tr>
</table>
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 184px; POSITION: absolute; TOP: 176px" runat="server" Text="注册"></asp:button>
<asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 176px" runat="server" Text="取消"></asp:button>
</form>
</body>
</html>
-------------------------------ASP.NET Ajax表单验证-.aspx.cs代码-----------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ajaxselect
{
/// <summary>
/// biaodanyan 的摘要说明。
/// </summary>
public class biaodanyan : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.HtmlControls.HtmlGenericControl error_tb_u_password;
protected System.Web.UI.HtmlControls.HtmlGenericControl error_tb_email;
protected System.Web.UI.HtmlControls.HtmlGenericControl error_tb_tel;
protected System.Web.UI.HtmlControls.HtmlGenericControl span_tb_email;
protected System.Web.UI.HtmlControls.HtmlGenericControl span_tb_tel;
protected System.Web.UI.HtmlControls.HtmlGenericControl span_tb_name;
protected System.Web.UI.HtmlControls.HtmlGenericControl error_tb_name;
protected System.Web.UI.HtmlControls.HtmlInputText tb_u_password;
protected System.Web.UI.HtmlControls.HtmlInputText tb_u_password2;
protected System.Web.UI.HtmlControls.HtmlInputText tb_tel;
protected System.Web.UI.HtmlControls.HtmlInputText tb_name;
protected System.Web.UI.HtmlControls.HtmlGenericControl span_tb_u_password2;
protected System.Web.UI.HtmlControls.HtmlGenericControl span_tb_u_password;
protected System.Web.UI.HtmlControls.HtmlGenericControl error_tb_u_password2;
protected System.Web.UI.HtmlControls.HtmlInputText tb_email;
private void Page_Load(object sender, System.EventArgs e)
{
//判断密码是否正确
tb_u_password.Attributes["onblur"] = "return onCheckPwd(this);";
//判断确认密码是否正确
tb_u_password2.Attributes["onblur"] = "return onComparePwd(this,tb_u_password);";
// //密码框获得焦点时
// tb_u_password.Attributes["onfocus"] = "return onFocusForm(this);";
//判断密码是否为纯数字
tb_u_password.Attributes["onblur"] = "return checkNum(this);";
//判断密码是否为纯字母
tb_u_password.Attributes["onblur"] = "return checkchinese(this);";
//判断用户名是否正确
tb_name.Attributes["onblur"] = "return CheckUid(this);";
//tb_name.Attributes["onfocus"] = "return onFocusForm(this);";
//判断邮箱是否正确
tb_email.Attributes["onblur"] = "return onCheckEmail(this);";
//tb_email.Attributes["onfocus"] = "return onFocusForm(this);";
//检查电话是否正确
tb_tel.Attributes["onblur"] = "return onCheckTel(this);";
//tb_tel.Attributes["onfocus"] = "return onFocusForm(this);";
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void tb_u_password2_TextChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("aaa");
}
private void tb_name_TextChanged(object sender, System.EventArgs e)
{
}
}
}