/** global sign.*/
var isDedug = true;
function NotNullRule(node,msg){
if( isDebug){
if( !node )
alert("IllegalArgument : node is null .");
}
this.node = node;
this.msg = msg;
NotNullRule.prototype.validate = function (){
var value = new String(this.node.value);
value = value.replace(/^/s*/g,'').replace(//s*$/g,'');
if( value =='')
throw this.msg;
return ;
}
return this;
}
function LengthRule(node,minLength,maxLength,msg){
if( isDebug){
if( !node )
alert("IllegalArgument : node is null .");
}
this.node=node;
this.minLen=minLength == null ? 0 : minLength ;
this.maxLen=maxLength == null ? 32767 : maxLength ;
this.msg = msg;
LengthRule.prototype.validate = function(){
var value = new String(this.node.value);
if( value.length < this.minLen || value.length > this.maxLen )
throw this.msg;
return;
}
return this;
}
function RegExRule(node, pattern , msg){
if( isDebug){
var str = '';
if( !node )
str += "IllegalArgument : node is null ./n";
if( !pattern)
str += "IllegalArgument : pattern is null./n";
var typeOfPattern = typeof(pattern);
if( typeOfPattern != "RegExp" && typeOfPattern != "string" )
str += "IllegalArgument : incorrent RegExp pattern ( parameter type is incorrect)./n";
if( str )
alert( "Error :/n"+ str);
}
this.node = node;
this.pattern = typeof(pattern) == "RegExp" ? pattern : new RegExp(pattern) ;
this.msg = msg;
RegExRule.prototype.validate= function(){
if ( this.node.value.split(this.pattern).length !=0 )
throw msg;
return ;
}
return this;
}
function CheckedRule(node,msg){
if( isDebug){
if( !node )
alert("IllegalArgument : node is null .");
}
this.node= node;
this.msg = msg;
CheckedRule.prototype.validate = function(){
if( this.node && this.node.nodeName=="SELECT" ){ // select .
var ops = this.node.options;
// must.
if ( !ops || ops.length ==0 )
return ;
for( var idx=0 ; idx < ops.length ; idx ++){
if( ops.item(idx).selected )
return ;
}
throw this.msg;
} else{ // radio group or checkbox group
var kids = document.getElementsByName(this.node.nodeName);
// must.
if( !kids || kids.length ==0 )
return ;
for( var i =0 ; i< kids.length ; i++ ){
if( kids[i].nodeName=="INPUT"
&& ( kids[i].type=="checkbox" || kids[i].type=="radio" )
&& kids[i].checked ){
return ;
}
}
throw this.msg ;
}
}
return this;
}
// Validator Controller.
function Ruler ( form ){
if( isDebug){
if( !node )
alert("IllegalArgument : form node is null .");
}
this.form = form;
this.rules = new Array();
// Delegate.
Ruler.prototype.addNotNull = function(node,msg){
this.rules.push(new NotNullRule(this.form[node],msg));
}
Ruler.prototype.addLength = function(node,minLen,maxLen,msg){
this.rules.push(new LengthRule(this.form[node],minLen,maxLen,msg));
}
Ruler.prototype.addRegEx = function(node,pattern,msg){
this.rules.push(new RegExRule(this.form[node],pattern,msg));
}
// global validation .
Ruler.prototype.validate = function(){
var msg="";
for( var i= 0; i< this.rules.length; i++){
try{
msg+= this.rules[i].validate();
}catch(e){
msg += e +"/n";
}
}
if( msg =="" ){
return true;
} else{
alert ("Invalid Input :/n/n"+ msg );
return false;
}
}
return this;
}