前些日子因为验证的问题而闹心,具体情况如下
1
,使用
javascript
,在按钮按下时直接执行服务器端的代码去了,不会执行
javascript;
2
,使用验证控件,验证控件占页面空间,有时候不好排版,当有两个以上按钮时,如一个提交,一个取消,按下取消的那个按钮也会执行验证,而这是不需要的。
经过一段时间学习和摸索,以上问题都得以解决,现将所知的验证方法总结如下:
一,使用
javascript
验证
其实在
asp.net
也可以使用
javascript
验证的,方法也很简单,只是要在提交按钮增一个属性,如例
1
。
例
1
:
<%
@ Page Language
=
"
C#
"
%>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< script runat = " server " >
protected void Page_Load( object sender, EventArgs e)
... {
btn.Attributes["onclick"] = "return check();";
}
protected void btn_Click( object sender, EventArgs e)
... {
Response.Write("验证成功");
}
</ script >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 例一 </ title >
< script type = " text/javascript " >
function check()
... {
if(document.form.tb.value == "")
...{
alert("不能为空");
document.form.tb.focus();
return false;
}
return true;
}
</ script >
</ head >
< body >
< form id = " form " runat = " server " >
< div >
< asp:TextBox runat = " server " ID = " tb " ></ asp:TextBox >
< asp:Button runat = " server " ID = " btn " OnClick = " btn_Click " Text = " 开始验证 " />
</ div >
</ form >
</ body >
</ html >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< script runat = " server " >
protected void Page_Load( object sender, EventArgs e)
... {
btn.Attributes["onclick"] = "return check();";
}
protected void btn_Click( object sender, EventArgs e)
... {
Response.Write("验证成功");
}
</ script >
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head runat = " server " >
< title > 例一 </ title >
< script type = " text/javascript " >
function check()
... {
if(document.form.tb.value == "")
...{
alert("不能为空");
document.form.tb.focus();
return false;
}
return true;
}
</ script >
</ head >
< body >
< form id = " form " runat = " server " >
< div >
< asp:TextBox runat = " server " ID = " tb " ></ asp:TextBox >
< asp:Button runat = " server " ID = " btn " OnClick = " btn_Click " Text = " 开始验证 " />
</ div >
</ form >
</ body >
</ html >
值得一提的这里的
tb
和
btn
是编译后的控件的
name,
而
value
也是编译后的控件的属性(
<asp:textbox>
没有
value
属性)。因空服务器控件会编译成相应的
html
控件。以下是部分对比:
<asp:Button > <input type=’submit’>
<asp:checkbox> <input type=’checkbox’>
<asp:checkboxlist> <input type=’checkbox’>
<asp:radiobutton> <input type=’radio’>
<asp:radiobuttonlist> <input type=’radio’>
<asp:image> <img>
<asp:imagebutton> <img>
<asp:hyperlink> <a>
<asp:linkbutton> <a>
<asp:dropdownlist> <select>
<asp:listbox> <select>
<asp:label> <span>
<asp:panel> <div>
所以使用
javascript
时要选择正确的属性。
二,
在服务器端验证
也挺简单的,如例
2
。
例
2:
<%
@ Page Language
=
"
C#
"
%>
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< script runat = " server " >
protected void btn_Click( object sender, EventArgs e)
... {
if (tb.Text == "")
...{
Response.Write("不能为空");
Response.End();
}
}
</ script >
<! DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< script runat = " server " >
protected void btn_Click( object sender, EventArgs e)
... {
if (tb.Text == "")
...{
Response.Write("不能为空");
Response.End();
}
}
</ script >
三,
使用验证控件
前面遇到的问题其实佷好解决,︿
_
︿。
其一:每一个验证控件都有一个
display
属性,属性值有:
static
:无论错误提示是否显示都占用网页空间。
Dynamic
:当错误提示不显示时不占网页空间,
none
:在
ValidationSummary
里显示。所以选择
Dynamic
可以解决一些版面的问题,但当错误显示时有时还是会破坏一些版面。
其二:
<asp:Button>
有一个属性
CausesValidation
,只要将它设为
false
就不会执行验证了。
1
,
验证控件有:
RequiredFieldValidator
:验证输入是否为空
CompareValidator
:可以比较两个控件的属性或一个控件与一个特定的值。
RangeValidator:
验证目标控件的值是否在指定的范围内
RegularExpressionValidator:
使用正则表达式,功能肯定不小
CustomValidator:
自定义验证控件
ValidationSummary:
列出所有没通过验证的控件的
ErrorMessage
值
验证控件可以验的的控件有:
HtmlInputText value
HtmlTextArea value
HtmlSelect value
HtmlInputFile value
TextBox value
ListBox SelectItem
DropDownList SelectItem
RadioButtonList SelectItem
2
,
正正则表达式:
^
字符串的开始
$
字符串的结束
[]
符号中表示可以接受的字符,
-
符号表示取值范围
,
[0-9]
表示可以接受
0-9
中的任意数字,如果在
[]
符号中用
^
符号,表示反向接受
{}
符号表示接受字符的个数。如
[a-z]{0,}
表示可以接受任意个小字母,
[a-z]{1,5}
表示可以接受
1
到
5
个小字字母,
[a-z]{4}
表示可以接受
4
个小字母。
/
符号表示匹配特定含义的字符
.
符号表示任意一个字符
()
符号表是将同一条件的表达式亿含起来
验证控件的内容较多,就不一一举例了,以后再使用中慢慢摸索了!