但是有時候我們又要因為使用者輸入了某些字元之後,我們就不進行驗證了,
所以就必須用到使用JavaScript來將Validator關閉了!
最近看到一個不錯的JavaScript的Library,可以很方便的幫助我們關閉他唷!
下面是它的Library,先將它Copy下來,然後存成.js檔後,include進入網頁就可以大方的使用摟~~
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
//Description:
// ValidationDefeater is a scipt file that provides the functionality to have more controle
// over ASP.NET 1.1 validation facilities.
//Author:
// Muhammad Shehabeddeen
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Contained Validators (No ControlToValidate)
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Changes the status of validation to [status] of all child controls of [control] if
// [control] is not null. If [control] is null, [status] is set for all validators.
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function traverseTree(status, control)
{
if(control == null)
{
for(var i = 0; i < Page_Validators.length; i++)
{
Page_Validators[i].enabled = status;
Page_Validators[i].style.display = status ? 'inline' : 'none';
}
}
else
{
//this is a way to check that the control is a validation control
if(control.evaluationfunction != null)
{
control.enabled = status;
control.style.display = status ? 'inline' : 'none';
}
for( var i=0; i < control.childNodes.length; i++)
{
traverseTree(status, control.childNodes[i]);
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Looks inside the control having [containerID] as its ID for all validators and enables them.
// If [containerID] is null (not provided) then all validators are enabled.
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function enableValidators(containerID)
{
var control;
if(containerID == null)
{
control = null;
}
else
{
control = document.getElementById(containerID);
}
if( containerID == null || control != null )
traverseTree(true, control);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Looks inside the control having [containerID] as its ID for all validators and disables them.
// If [containerID] is null (not provided) then all validators are disabled.
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function disableValidators(containerID)
{
var control;
if(containerID == null)
{
control = null;
}
else
{
control = document.getElementById(containerID);
}
if( containerID == null || control != null )
traverseTree(false, control);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// END Contained Validators (No ControlToValidate)
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Contained Validators With ControlToValidate
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Changes the status of validation to [status] of all child controls of [control] that
// have ControlToValidate of value [controlToValidateID]
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function traverseTreeCTV(status, controlToValidateID, control)
{
if(control == null)
{
for(var i = 0; i < Page_Validators.length; i++)
{
if(Page_Validators[i].controltovalidate != null &&
Page_Validators[i].controltovalidate == controlToValidateID)
{
Page_Validators[i].enabled = status;//disable validator
Page_Validators[i].style.display = status ? 'inline' : 'none';
}
}
}
else
{
if(control.controltovalidate != null && control.controltovalidate == controlToValidateID)
{
control.enabled = status;//disable validator
control.style.display = status ? 'inline' : 'none';
}
for( var i=0; i < control.childNodes.length; i++)
{
traverseTreeCTV(status, controlToValidateID, control.childNodes[i]);
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Looks inside the control having [containerID] as its ID for any validators that have the
// ControlToValidate property of value [controlToValidateID] and enables them. So it enables validators of
// [controlToValidateID] inside [containerID].
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function enableCTVValidators(controlToValidateID, containerID)
{
var control;
if(containerID == null)
{
control = null;
}
else
{
control = document.getElementById(containerID);
}
if( containerID == null || control != null )
traverseTreeCTV(true, controlToValidateID, control );
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Looks inside the control having [containerID] as its ID for any validators that have the
// ControlToValidate property of value [controlToValidateID] and disables them. So it disables validators of
// [controlToValidateID] inside [containerID].
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function disableCTVValidators(controlToValidateID, containerID)
{
var control;
if(containerID == null)
{
control = null;
}
else
{
control = document.getElementById(containerID);
}
if( containerID == null || control != null )
traverseTreeCTV(false, controlToValidateID, control );
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// END Contained Validators With ControlToValidate
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Group Validators
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Looks for all validators having a groupID of [groupID] and sets their status to [status]
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function setGroupValidatorsStatus(groupID, status)
{
for(var i = 0; i < Page_Validators.length; i++)
{
if(Page_Validators[i].attributes['groupID'].value == groupID)
{
Page_Validators[i].enabled = status;
Page_Validators[i].style.display = status ? 'inline' : 'none';
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Enables all validators having a groupID of [groupID]
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function enableGroupValidators(groupID)
{
setGroupValidatorsStatus(groupID, true)
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Disables all validators having a groupID of [groupID]
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
function disableGroupValidators(groupID)
{
setGroupValidatorsStatus(groupID, false)
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// END Group Validators
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// Show/Hide
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
//hides Validators but does not disable them
function hideValidators(containerID)
{
var control;
if(containerID == null)
{
control = null;
}
else
{
control = document.getElementById(containerID);
}
if( containerID == null || control != null )
traverseTreeShowHide(false, control);
}
//shows validators but does not enable them
function showValidators(containerID)
{
var control;
if(containerID == null)
{
control = null;
}
else
{
control = document.getElementById(containerID);
}
if( containerID == null || control != null )
traverseTreeShowHide(true, control);
}
function traverseTreeShowHide(status, control)
{
if(control == null)
{
for(var i = 0; i < Page_Validators.length; i++)
{
Page_Validators[i].style.display = status ? 'inline' : 'none';
}
}
else
{
//this is a way to check that the control is a validation control
if(control.evaluationfunction != null)
{
control.style.display = status ? 'inline' : 'none';
}
for( var i=0; i < control.childNodes.length; i++)
{
traverseTreeShowHide(status, control.childNodes[i]);
}
}
}
//hides the containerID control and if [alsoDisable] is true all validators inside are disabled.
//If the containerID is not supplied, then all validators in the page are hidden.
function hide(alsoDisable, containerID )
{
if(alsoDisable == null)
alsoDisable = false;
var container;
if(containerID == null)
{
container = null;
}
else
{
container = document.getElementById(containerID);
}
if(containerID == null || container != null)
{
if(container != null)
container.style.display = 'none';
if(alsoDisable)
disableValidators(containerID);
else
hideValidators(containerID);
}
}
//does the opposite of hide()
function show(alsoEnable, containerID)
{
if(alsoEnable == null)
alsoEnable = false;
var container;
if(containerID == null)
{
container = null;
}
else
{
container = document.getElementById(containerID);
}
if(containerID == null || container != null)
{
if(container != null)
container.style.display = 'block';
if(alsoEnable)
enableValidators(containerID);
else
showValidators(containerID);
}
}
//flips the state of the containerID control
function showHide(containerID)
{
var container;
if(containerID == null)
{
container = null;
}
else
{
container = document.getElementById(containerID);
}
if(containerID == null || container != null)
{
if(container.style.display == null)
{
hide(containerID);
}
else if(control.style.display == 'none')
{
show(containerID);
}
else
{
hide(containerID);
}
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
// END Show/Hide
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*/
使用方法也很簡單唷!
關閉驗證
disableValidators('控制項的ClientId');
開啟驗證
enableValidators('控制項的ClientId');
沒有留言:
張貼留言