为了实现验证,.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文件中相应的方法都移到本页面了)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
无标题页
</title></head>
<body>
<form>
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE5MDkxNzgwODFkZKLnaM8iGUsgGiWldhYd7fCPmt6s" />
</div>
<div>
<input name="TextBox1" type="text" id="TextBox1" />
<span id="RequiredFieldValidator1" style="color:Red;display:none;">RequiredFieldValidator</span></div>
<script type="text/javascript">
<!--

//页面加载时执行的方法
var Page_ValidationVer = "125";
var Page_IsValid = true;
var Page_BlockSubmit = false;
var Page_InvalidControlToBeFocused = null;


function ValidatorOnLoad()
{
if (typeof(Page_Validators) == "undefined")
return;
var i, val;

for (i = 0; i < Page_Validators.length; i++)
{
val = Page_Validators[i];

if (typeof(val.evaluationfunction) == "string")
{
eval("val.evaluationfunction = " + val.evaluationfunction + ";");
}

if (typeof(val.isvalid) == "string")
{

if (val.isvalid == "False")
{
val.isvalid = false;
Page_IsValid = false;
}

else
{
val.isvalid = true;
}

} else
{
val.isvalid = true;
}

if (typeof(val.enabled) == "string")
{
val.enabled = (val.enabled != "False");
}

if (typeof(val.controltovalidate) == "string")
{
ValidatorHookupControlID(val.controltovalidate, val);
}

if (typeof(val.controlhookup) == "string")
{
ValidatorHookupControlID(val.controlhookup, val);
}
}
Page_ValidationActive = true;
}


function ValidatorHookupControlID(controlID, val)
{

if (typeof(controlID) != "string")
{
return;
}
var ctrl = document.getElementById(controlID);

if ((typeof(ctrl) != "undefined") && (ctrl != null))
{
ValidatorHookupControl(ctrl, val);
}

else
{
val.isvalid = true;
val.enabled = false;
}
}

function ValidatorHookupControl(control, val)
{

if (typeof(control.tagName) != "string")
{
return;
}

if (control.tagName != "INPUT" && control.tagName != "TEXTAREA" && control.tagName != "SELECT")
{
var i;

for (i = 0; i < control.childNodes.length; i++)
{
ValidatorHookupControl(control.childNodes[i], val);
}
return;
}

else
{

if (typeof(control.Validators) == "undefined")
{
control.Validators = new Array;
var eventType;

if (control.type == "radio")
{
eventType = "onclick";

} else
{
eventType = "onchange";

if (typeof(val.focusOnError) == "string" && val.focusOnError == "t")
{
ValidatorHookupEvent(control, "onblur", "ValidatedControlOnBlur(event); ");
}
}
ValidatorHookupEvent(control, eventType, "ValidatorOnChange(event); ");
if (control.type == "text" ||
control.type == "password" ||

control.type == "file")
{
ValidatorHookupEvent(control, "onkeypress",
"if (!ValidatedTextBoxOnKeyPress(event)) { event.cancelBubble = true; if (event.stopPropagation) event.stopPropagation(); return false; } ");
}
}
control.Validators[control.Validators.length] = val;
}
}

function ValidatorHookupEvent(control, eventType, functionPrefix)
{
var ev;
eval("ev = control." + eventType + ";");

if (typeof(ev) == "function")
{
ev = ev.toString();
ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
}

else
{
ev = "";
}
var func;

if (navigator.appName.toLowerCase().indexOf('explorer') > -1)
{
func = new Function(functionPrefix + " " + ev);
}

else
{
func = new Function("event", functionPrefix + " " + ev);
}
eval("control." + eventType + " = func;");
}

//按键事件

function ValidatedTextBoxOnKeyPress(event)
{


if (event.keyCode == 13)
{
ValidatorOnChange(event);
var vals;

if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null))
{
vals = event.srcElement.Validators;
}

else
{
vals = event.target.Validators;
}
return AllValidatorsValid(vals);
}
return true;
}

//离开焦点依次执行以下方法

function ValidatorOnChange(event)
{

if (!event)
{
event = window.event;
}
Page_InvalidControlToBeFocused = null;
var targetedControl;

if ((typeof(event.srcElement) != "undefined") && (event.srcElement != null))
{
targetedControl = event.srcElement;
}

else
{
targetedControl = event.target;
}
var vals;

if (typeof(targetedControl.Validators) != "undefined")
{
vals = targetedControl.Validators;
}

else
{

if (targetedControl.tagName.toLowerCase() == "label")
{
targetedControl = document.getElementById(targetedControl.htmlFor);
vals = targetedControl.Validators;
}
}
var i;

for (i = 0; i < vals.length; i++)
{
ValidatorValidate(vals[i], null, event);
}
ValidatorUpdateIsValid();
}


function ValidatorValidate(val, validationGroup, event)
{
val.isvalid = true;

if ((typeof(val.enabled) == "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup))
{

if (typeof(val.evaluationfunction) == "function")
{
val.isvalid = val.evaluationfunction(val);
if (!val.isvalid && Page_InvalidControlToBeFocused == null &&

typeof(val.focusOnError) == "string" && val.focusOnError == "t")
{
ValidatorSetFocus(val, event);
}
}
}
ValidatorUpdateDisplay(val);
}

function IsValidationGroupMatch(control, validationGroup)
{

if ((typeof(validationGroup) == "undefined") || (validationGroup == null))
{
return true;
}
var controlGroup = "";

if (typeof(control.validationGroup) == "string")
{
controlGroup = control.validationGroup;
}
return (controlGroup == validationGroup);
}

function RequiredFieldValidatorEvaluateIsValid(val)
{
return (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))
}


function ValidatorGetValue(id)
{
var control;
control = document.getElementById(id);

if (typeof(control.value) == "string")
{
return control.value;
}
return ValidatorGetValueRecursive(control);
}

function ValidatorTrim(s)
{
var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
return (m == null) ? "" : m[1];
}

function ValidatorUpdateDisplay(val)
{

if (typeof(val.display) == "string")
{

if (val.display == "None")
{
