Pure Javascript validation

Pure JavaScript functions for form validation, character counter, int validation, float validation, email validation, multiple SelectBox validation-


Here are few functions which can be used in any application for javascript validation. Since they don't require any library like jQuery to function they are very lightweight.

<script type="text/javascript">
//remove white spaces
function trim(s){
var l=0;
        var r=s.length-1;
while(l<s.length && s[l]==' '){
           l++;
        }
while(r>l && s[r]==' '){
           r-=1;
        }
return s.substring(l,r+1);
}

//email validation with regular expression
function checkEmail(email2) {
  var regMail = /^([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,3})$/;
  if(regMail.test(email2)==false){
     return false;
  } else {
   return true;
  }
}

// example of form validation. call in <form onsubmit="return validate(this)">
function validate(obj){
   if(trim(obj.name.value)==''){
      alert("Please Enter Name");
      obj.name.focus();
      return false;
} else if(trim(obj.email.value)==''){
      alert("Please Enter Email");
      obj.email.focus();
      return false;
} else if(!checkEmail(obj.email.value)){
      alert("Please provide valid Email");
      obj.email.focus();
      return false;
} else {
   return true;
 }
}

//string length counter in textbox or textarea

function textCounter(fieldid, countfield){
   field=document.getElementById(fieldid);
   var maxlimit=500;
   if (field.value.length > maxlimit) 
      field.value = field.value.substring(0, maxlimit);  
    else document.getElementById(countfield).innerHTML= maxlimit - field.value.length;
}

//Regular exepression validation for int validation
function test1(str){
return /^ *[0-9]+ *$/.test(str);
}

//Regular exepression validation for floatvalidation
function validateDecimal(value){
        return /^\d*\.?\d*$/.test(value);
 }

/// keyCode validation for float values
function check_float(e,field){
     if (!(((e.keyCode>=48)&&(e.keyCode<=57))||(e.keyCode==46))){
        e.keyCode=0;  
        return false;              
     }     
     if (e.keyCode==46){
        var patt1=new RegExp("\\.");        
        var ch =patt1.exec(field);       
        if(ch=="."){
           e.keyCode=0;return false;
        }                        
     }
   return true;
 } 

//keyCode validation for numeric values can be enter in text box
function isNumberKey(evt) {
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
 return true;
  }

//multiple select box validation
function validate(obj){
   if(document.frm["state[]"].value == ""){
       alert('Please choose State');
       return false; 
  }
   return true;
}


 // or multiple select box validation
 function validate(obj){
      var expr=0;
      for(ex=0;ex<obj.elements['ex_name[]'].length;ex++){
        if(obj.elements['ex_name[]'][ex].selected==true){
  expr=1;
}
  }
       if(expr==0){
    alert("Please Select Name of Consignor / Exporter!");
return false;
    }
 }

</script>

Post a Comment