博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.Net中验证控件简单分析
阅读量:6569 次
发布时间:2019-06-24

本文共 8764 字,大约阅读时间需要 29 分钟。

为了实现验证,.net提供了一个javascript脚本,当我们生成一个asp.net页面时,通过查看页面源代码的方式,会发现都调用了下面一个javascript文件:

<script src="WebResource.axd" type="text/javascript"> </script>
asp.net的大部分验证都是通过这个文件进行的,下面我就以一个最简单的必入项验证控件来说明一下:

第一步:

当进入页面时首先赋值:var Page_Validators =  new Array(document.getElementById("RequiredFieldValidator1"));
然后会根据这个变量的值遍历进行验证,
接着调用ValidatorOnLoad方法,然后在这个方法中调用ValidatorHookupControlID,ValidatorHookupControl,ValidatorHookupEvent

最主要是在ValidatorHookupEvent中给控件加载事件    

eval("control." + eventType + " = func;");

比如onblue,onkeypress,onchange等事件

接下来根据加载的事件当事件发生时调用对应的方法

第二步:

当用户输入时调用ValidatedTextBoxOnKeyPress,主要是用来判断用户是否输入了回车键,如果是的话触发验证

第三步:

当用户输入回车或者离开焦点时调用以下方法进行验证:
ValidatorOnChange,ValidatorValidate,IsValidationGroupMatch,RequiredFieldValidatorEvaluateIsValid,ValidatorGetValue,
ValidatorTrim,ValidatorUpdateDisplay,ValidatorUpdateIsValid,AllValidatorsValid
主要就是根据用户的输入进行判断,决定是否显示错误信息:val.style.display = val.isvalid ? "none" : "inline";

附修改后的带有一个必入项验证的页面源码(将WebResource.axd文件中相应的方法都移到本页面了)

None.gif
<!
DOCTYPE html PUBLIC 
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
 
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
None.gif
None.gif
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
 
>
None.gif
<
head
><
title
>
None.gif    无标题页
None.gif
</
title
></
head
>
None.gif
<
body
>
None.gif    
<
form
>
None.gif
<
div
>
None.gif
<
input type
=
"
hidden
"
 name
=
"
__VIEWSTATE
"
 id
=
"
__VIEWSTATE
"
 value
=
"
/wEPDwULLTE5MDkxNzgwODFkZKLnaM8iGUsgGiWldhYd7fCPmt6s
"
 
/>
None.gif
</
div
>
None.gif   
<
div
>
None.gif        
<
input name
=
"
TextBox1
"
 type
=
"
text
"
 id
=
"
TextBox1
"
 
/>
None.gif        
<
span id
=
"
RequiredFieldValidator1
"
 style
=
"
color:Red;display:none;
"
>
RequiredFieldValidator
</
span
></
div
>
None.gif    
None.gif
<
script type
=
"
text/javascript
"
>
None.gif
<!--
None.gif
None.gif
//
页面加载时执行的方法
None.gif
var Page_ValidationVer 
=
 
"
125
"
;
None.gifvar Page_IsValid 
=
 
true
;
None.gifvar Page_BlockSubmit 
=
 
false
;
None.gifvar Page_InvalidControlToBeFocused 
=
 
null
;
None.gif
ExpandedBlockStart.giffunction ValidatorOnLoad() 
{
InBlock.gif    
if
 (
typeof
(Page_Validators) 
==
 
"
undefined
"
)
InBlock.gif        
return
;
InBlock.gif    var i, val;
ExpandedSubBlockStart.gif    
for
 (i 
=
 
0
; i 
<
 Page_Validators.length; i
++
{
InBlock.gif        val 
=
 Page_Validators[i];
ExpandedSubBlockStart.gif        
if
 (
typeof
(val.evaluationfunction) 
==
 
"
string
"
{
InBlock.gif            eval(
"
val.evaluationfunction = 
"
 
+
 val.evaluationfunction 
+
 
"
;
"
);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif        
if
 (
typeof
(val.isvalid) 
==
 
"
string
"
{
ExpandedSubBlockStart.gif            
if
 (val.isvalid 
==
 
"
False
"
{
InBlock.gif                val.isvalid 
=
 
false
;
InBlock.gif                Page_IsValid 
=
 
false
;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockStart.gif            
else
 
{
InBlock.gif                val.isvalid 
=
 
true
;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockStart.gif        }
 
else
 
{
InBlock.gif            val.isvalid 
=
 
true
;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif        
if
 (
typeof
(val.enabled) 
==
 
"
string
"
{
InBlock.gif            val.enabled 
=
 (val.enabled 
!=
 
"
False
"
);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif        
if
 (
typeof
(val.controltovalidate) 
==
 
"
string
"
{
InBlock.gif            ValidatorHookupControlID(val.controltovalidate, val);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif        
if
 (
typeof
(val.controlhookup) 
==
 
"
string
"
{
InBlock.gif            ValidatorHookupControlID(val.controlhookup, val);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    Page_ValidationActive 
=
 
true
;
ExpandedBlockEnd.gif}
None.gif
ExpandedBlockStart.giffunction ValidatorHookupControlID(controlID, val) 
{
ExpandedSubBlockStart.gif    
if
 (
typeof
(controlID) 
!=
 
"
string
"
{
InBlock.gif        
return
;
ExpandedSubBlockEnd.gif    }
InBlock.gif    var ctrl 
=
 document.getElementById(controlID);
ExpandedSubBlockStart.gif    
if
 ((
typeof
(ctrl) 
!=
 
"
undefined
"
&&
 (ctrl 
!=
 
null
)) 
{
InBlock.gif        ValidatorHookupControl(ctrl, val);
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
else
 
{
InBlock.gif        val.isvalid 
=
 
true
;
InBlock.gif        val.enabled 
=
 
false
;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction ValidatorHookupControl(control, val) 
{
ExpandedSubBlockStart.gif   
if
 (
typeof
(control.tagName) 
!=
 
"
string
"
{
InBlock.gif        
return
;  
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
if
 (control.tagName 
!=
 
"
INPUT
"
 
&&
 control.tagName 
!=
 
"
TEXTAREA
"
 
&&
 control.tagName 
!=
 
"
SELECT
"
{
InBlock.gif        var i;
ExpandedSubBlockStart.gif        
for
 (i 
=
 
0
; i 
<
 control.childNodes.length; i
++
{
InBlock.gif            ValidatorHookupControl(control.childNodes[i], val);
ExpandedSubBlockEnd.gif        }
InBlock.gif        
return
;
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
else
 
{
ExpandedSubBlockStart.gif        
if
 (
typeof
(control.Validators) 
==
 
"
undefined
"
{
InBlock.gif            control.Validators 
=
 
new
 Array;
InBlock.gif            var eventType;
ExpandedSubBlockStart.gif            
if
 (control.type 
==
 
"
radio
"
{
InBlock.gif                eventType 
=
 
"
onclick
"
;
ExpandedSubBlockStart.gif            }
 
else
 
{
InBlock.gif                eventType 
=
 
"
onchange
"
;
ExpandedSubBlockStart.gif                
if
 (
typeof
(val.focusOnError) 
==
 
"
string
"
 
&&
 val.focusOnError 
==
 
"
t
"
{
InBlock.gif                    ValidatorHookupEvent(control, 
"
onblur
"
"
ValidatedControlOnBlur(event); 
"
);
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            ValidatorHookupEvent(control, eventType, 
"
ValidatorOnChange(event); 
"
);
InBlock.gif            
if
 (control.type 
==
 
"
text
"
 
||
InBlock.gif                control.type 
==
 
"
password
"
 
||
ExpandedSubBlockStart.gif                control.type 
==
 
"
file
"
{
InBlock.gif                ValidatorHookupEvent(control, 
"
onkeypress
"
InBlock.gif                    
"
if (!ValidatedTextBoxOnKeyPress(event)) { event.cancelBubble = true; if (event.stopPropagation) event.stopPropagation(); return false; } 
"
);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        control.Validators[control.Validators.length] 
=
 val;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction ValidatorHookupEvent(control, eventType, functionPrefix) 
{
InBlock.gif    var ev;
InBlock.gif    eval(
"
ev = control.
"
 
+
 eventType 
+
 
"
;
"
);
ExpandedSubBlockStart.gif    
if
 (
typeof
(ev) 
==
 
"
function
"
{
InBlock.gif        ev 
=
 ev.toString();
InBlock.gif        ev 
=
 ev.substring(ev.indexOf(
"
{
"
+
 
1
, ev.lastIndexOf(
"
}
"
));
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
else
 
{
InBlock.gif        ev 
=
 
""
;
ExpandedSubBlockEnd.gif    }
InBlock.gif    var func;
ExpandedSubBlockStart.gif    
if
 (navigator.appName.toLowerCase().indexOf(
'
explorer
'
>
 
-
1
{
InBlock.gif        func 
=
 
new
 Function(functionPrefix 
+
 
"
 
"
 
+
 ev);
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
else
 
{
InBlock.gif        func 
=
 
new
 Function(
"
event
"
, functionPrefix 
+
 
"
 
"
 
+
 ev);
ExpandedSubBlockEnd.gif    }
InBlock.gif    eval(
"
control.
"
 
+
 eventType 
+
 
"
 = func;
"
);
ExpandedBlockEnd.gif}
None.gif
None.gif
//
按键事件
ExpandedBlockStart.gif
function ValidatedTextBoxOnKeyPress(
event
{
InBlock.gif
ExpandedSubBlockStart.gif   
if
 (
event
.keyCode 
==
 
13
{
InBlock.gif        ValidatorOnChange(
event
);
InBlock.gif        var vals;
ExpandedSubBlockStart.gif        
if
 ((
typeof
(
event
.srcElement) 
!=
 
"
undefined
"
&&
 (
event
.srcElement 
!=
 
null
)) 
{
InBlock.gif            vals 
=
 
event
.srcElement.Validators;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif        
else
 
{
InBlock.gif            vals 
=
 
event
.target.Validators;
ExpandedSubBlockEnd.gif        }
InBlock.gif        
return
 AllValidatorsValid(vals);
ExpandedSubBlockEnd.gif    }
InBlock.gif    
return
 
true
;
ExpandedBlockEnd.gif}
None.gif
None.gif
//
离开焦点依次执行以下方法
ExpandedBlockStart.gif
function ValidatorOnChange(
event
{
ExpandedSubBlockStart.gif   
if
 (
!
event
{
InBlock.gif        
event
 
=
 window.
event
;
ExpandedSubBlockEnd.gif    }
InBlock.gif    Page_InvalidControlToBeFocused 
=
 
null
;
InBlock.gif    var targetedControl;
ExpandedSubBlockStart.gif    
if
 ((
typeof
(
event
.srcElement) 
!=
 
"
undefined
"
&&
 (
event
.srcElement 
!=
 
null
)) 
{
InBlock.gif        targetedControl 
=
 
event
.srcElement;
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
else
 
{
InBlock.gif        targetedControl 
=
 
event
.target;
ExpandedSubBlockEnd.gif    }
InBlock.gif    var vals;
ExpandedSubBlockStart.gif    
if
 (
typeof
(targetedControl.Validators) 
!=
 
"
undefined
"
{
InBlock.gif        vals 
=
 targetedControl.Validators;
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
else
 
{
ExpandedSubBlockStart.gif        
if
 (targetedControl.tagName.toLowerCase() 
==
 
"
label
"
{
InBlock.gif            targetedControl 
=
 document.getElementById(targetedControl.htmlFor);
InBlock.gif            vals 
=
 targetedControl.Validators;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    var i;
ExpandedSubBlockStart.gif    
for
 (i 
=
 
0
; i 
<
 vals.length; i
++
{
InBlock.gif        ValidatorValidate(vals[i], 
null
event
);
ExpandedSubBlockEnd.gif    }
InBlock.gif    ValidatorUpdateIsValid();
ExpandedBlockEnd.gif}
None.gif
ExpandedBlockStart.giffunction ValidatorValidate(val, validationGroup, 
event
{
InBlock.gif    val.isvalid 
=
 
true
;
ExpandedSubBlockStart.gif    
if
 ((
typeof
(val.enabled) 
==
 
"
undefined
"
 
||
 val.enabled 
!=
 
false
&&
 IsValidationGroupMatch(val, validationGroup)) 
{
ExpandedSubBlockStart.gif        
if
 (
typeof
(val.evaluationfunction) 
==
 
"
function
"
{
InBlock.gif            val.isvalid 
=
 val.evaluationfunction(val);
InBlock.gif            
if
 (
!
val.isvalid 
&&
 Page_InvalidControlToBeFocused 
==
 
null
 
&&
ExpandedSubBlockStart.gif                
typeof
(val.focusOnError) 
==
 
"
string
"
 
&&
 val.focusOnError 
==
 
"
t
"
{
InBlock.gif                ValidatorSetFocus(val, 
event
);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    ValidatorUpdateDisplay(val);
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction IsValidationGroupMatch(control, validationGroup) 
{
ExpandedSubBlockStart.gif   
if
 ((
typeof
(validationGroup) 
==
 
"
undefined
"
||
 (validationGroup 
==
 
null
)) 
{
InBlock.gif        
return
 
true
;
ExpandedSubBlockEnd.gif    }
InBlock.gif    var controlGroup 
=
 
""
;
ExpandedSubBlockStart.gif    
if
 (
typeof
(control.validationGroup) 
==
 
"
string
"
{
InBlock.gif        controlGroup 
=
 control.validationGroup;
ExpandedSubBlockEnd.gif    }
InBlock.gif    
return
 (controlGroup 
==
 validationGroup);
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction RequiredFieldValidatorEvaluateIsValid(val) 
{
InBlock.gif    
return
 (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) 
!=
 ValidatorTrim(val.initialvalue))
ExpandedBlockEnd.gif}
None.gif
ExpandedBlockStart.giffunction ValidatorGetValue(id) 
{
InBlock.gif    var control;
InBlock.gif    control 
=
 document.getElementById(id);
ExpandedSubBlockStart.gif    
if
 (
typeof
(control.value) 
==
 
"
string
"
{
InBlock.gif        
return
 control.value;
ExpandedSubBlockEnd.gif    }
InBlock.gif    
return
 ValidatorGetValueRecursive(control);
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction ValidatorTrim(s) 
{
InBlock.gif    var m 
=
 s.match(
/^
\s
*
(\S
+
(\s
+
\S
+
)
*
)\s
*
$
/
);
InBlock.gif    
return
 (m 
==
 
null
?
 
""
 : m[
1
];
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction ValidatorUpdateDisplay(val) 
{
ExpandedSubBlockStart.gif    
if
 (
typeof
(val.display) 
==
 
"
string
"
{
ExpandedSubBlockStart.gif        
if
 (val.display 
==
 
"
None
"
{
InBlock.gif            
return
;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gif        
if
 (val.display 
==
 
"
Dynamic
"
{
InBlock.gif            val.style.display 
=
 val.isvalid 
?
 
"
none
"
 : 
"
inline
"
;
InBlock.gif            
return
;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    
if
 ((navigator.userAgent.indexOf(
"
Mac
"
>
 
-
1
&&
ExpandedSubBlockStart.gif        (navigator.userAgent.indexOf(
"
MSIE
"
>
 
-
1
)) 
{
InBlock.gif        val.style.display 
=
 
"
inline
"
;
ExpandedSubBlockEnd.gif    }
InBlock.gif    val.style.visibility 
=
 val.isvalid 
?
 
"
hidden
"
 : 
"
visible
"
;
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction ValidatorUpdateIsValid() 
{
InBlock.gif    Page_IsValid 
=
 AllValidatorsValid(Page_Validators);
ExpandedBlockEnd.gif}
ExpandedBlockStart.giffunction AllValidatorsValid(validators) 
{
ExpandedSubBlockStart.gif   
if
 ((
typeof
(validators) 
!=
 
"
undefined
"
&&
 (validators 
!=
 
null
)) 
{
InBlock.gif        var i;
ExpandedSubBlockStart.gif        
for
 (i 
=
 
0
; i 
<
 validators.length; i
++
{
ExpandedSubBlockStart.gif            
if
 (
!
validators[i].isvalid) 
{
InBlock.gif                
return
 
false
;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif    
return
 
true
;
ExpandedBlockEnd.gif}
None.gif
None.gifvar Page_Validators 
=
  
new
 Array(document.getElementById(
"
RequiredFieldValidator1
"
));
None.gif
//
 -->
None.gif
</
script
>
None.gif
None.gif
<
script type
=
"
text/javascript
"
>
None.gif
<!--
None.gifvar RequiredFieldValidator1 
=
 document.all 
?
 document.all[
"
RequiredFieldValidator1
"
] : document.getElementById(
"
RequiredFieldValidator1
"
);
None.gifRequiredFieldValidator1.controltovalidate 
=
 
"
TextBox1
"
;
None.gifRequiredFieldValidator1.errormessage 
=
 
"
RequiredFieldValidator
"
;
None.gifRequiredFieldValidator1.display 
=
 
"
Dynamic
"
;
None.gifRequiredFieldValidator1.evaluationfunction 
=
 
"
RequiredFieldValidatorEvaluateIsValid
"
;
None.gifRequiredFieldValidator1.initialvalue 
=
 
""
;
None.gif
//
 -->
None.gif
</
script
>
None.gif
None.gif
<
div
>
None.gif
None.gif    
<
input type
=
"
hidden
"
 name
=
"
__EVENTVALIDATION
"
 id
=
"
__EVENTVALIDATION
"
 value
=
"
/wEWAgLl/7L9DwLs0bLrBmRIrZczy6wbyzMNEvjnHwL+7PHV
"
 
/>
None.gif
</
div
>
None.gif
None.gif
<
script type
=
"
text/javascript
"
>
None.gif
<!--
None.gifvar Page_ValidationActive 
=
 
false
;
ExpandedBlockStart.gif
if
 (
typeof
(ValidatorOnLoad) 
==
 
"
function
"
{
InBlock.gif    ValidatorOnLoad();
ExpandedBlockEnd.gif}
None.gif
//
 -->
None.gif
</
script
>
None.gif        
</
form
>
None.gif
</
body
>
None.gif
</
html
>
None.gif
    本文转自永春博客园博客,原文链接:
http://www.cnblogs.com/firstyi/archive/2007/03/28/691061.html
,如需转载请自行联系原作者
你可能感兴趣的文章
【转】解决 java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I 问题
查看>>
Winform中添加客户信息
查看>>
JS的Date对象
查看>>
做好自己该做的,赢取自己这一份努力
查看>>
20145222黄亚奇《Java程序设计》第8周学习总结
查看>>
DTCMS插件的制作实例电子资源管理(四)URL重写
查看>>
Win2D 官方文章系列翻译 - 避免内存泄漏
查看>>
【C++】函数指针的嵌套
查看>>
P1156 垃圾陷阱
查看>>
MySQL开发遇到的问题
查看>>
P1339 热浪 最短路径模板题
查看>>
简明现代魔法博客图书馆之php学习记录
查看>>
深入了解java线程池
查看>>
API接口自动化之2 处理http请求的返回体,对返回体做校验
查看>>
六种方法实现CSS三栏布局
查看>>
AIO-3128C四核高性能主板
查看>>
代码文件的编码不统一导致的坑
查看>>
20145240《信息安全系统设计基础》第十二周学习总结
查看>>
物理机_双机调试_资料
查看>>
slice,substr和substring的区别
查看>>