WebScript - JavaScript Validate Rule

/** 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值