Can anyone please help me to simplify this form validation script? It works great but I was just wondering if I can get some help to make it simpler. Your opinion on the approach I used below is also welcome (i.e it is good or bad practice/method).
<script type="text/javascript">
var FormValidation = function(form){
this.messages = {
required : 'This field should not be empty',
email : 'Please enter valid email',
number : 'Please enter valid number',
min : 'This field length should be minimum ',
max : 'This field length should not exceed ',
range : 'This field length between '
};
validator = this;
var currentmsg =this;
this.required = function(value){
var valid = (value.length > 0);
return {status: valid, message: valid ? '' : currentmsg.messages.required};
}
this.email = function(value){
var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ ;
var valid = pattern.test(value);
return { status:valid, message: valid ? '' : currentmsg.messages.email};
}
this.number = function(value){
var pattern = /^[0-9]+/ ;
var valid = pattern.test(value);
return { status:valid, message: valid ? '' : currentmsg.messages.number};
}
this.min = function(value,args){
if(value.length > 0){
var valid = (value.length >= args[0])
return { status:valid, message: valid ? '' : currentmsg.messages.min + args[0] };
}
}
this.max = function(value,args){
if(value.length > 0){
var valid = (value.length <= args[0])
return { status:valid, message: valid ? '' : currentmsg.messages.max + args[0] };
}
}
this.range = function(value,args){
var valid = (value.length >= args[0] && value.length <= args[1])
return {
status:valid,
message: valid ? '' : currentmsg.messages.range + args[0] + ' and ' + args[1]
};
}
this.validators = {
required : validator.required,
email : validator.email,
number : validator.number,
range : validator.range,
max : validator.max,
min : validator.min
};
this.validate = function(form){
var elements = form.elements;
var status = true;
for(var i = 0; i < elements.length ; i++){
var validate = elements[i].getAttribute('validate');
if(!validate){
continue;
}
var types = validate.split(' ');
for(var j = 0; j < types.length; j++){
var result = this.validateField(elements[i], types[j]);
if(!result) {
continue
}
this.displayErrors(elements[i], result);
if(!result.status) {
status = false;
break;
}
}
}
return status;
}
this.displayErrors = function(element, result){
element.className = result.status ? '' : 'error';
var elErr =element.errorMsg;
if(elErr!=null)
elErr.className = result.status ? '' :'valerr'
if(!element.errorMsg){
elErr = document.createElement("div");
elErr.id = 'valerr';
element.parentNode.appendChild(elErr);
element.errorMsg = elErr;
}
elErr.innerHTML = result.message;
}
this.validateField = function (element, type){
var validationType = type;
var args ;
if(type.indexOf("(")!= -1 && type.indexOf(")") != -1){
var result = this.getArguments(type);
validationType = result.type;
args = result.argsList;
}
validator = this.validators[validationType];
if(validator != null){
return validator(element.value ,args);
}
return null;
}
this.getArguments = function(type){
var validationtype = type.substring(0,type.indexOf("("));
var args = type.substring(type.indexOf("(")+1,type.indexOf(")")).split(',');
return { type : validationtype, argsList : args}
}
this.init = function() {
var curForm = this;
var forms = document.forms;
for(var i = 0; i < forms.length ; i++){
if(forms[i].getAttribute('validate')!=null){
forms[i].onsubmit = function(){
return curForm.validate(this);
};
}
}
}
}
window.onload = function() {
var formValidation = new FormValidation();
formValidation.init();
}
</script>
<form method="post" action="#" validate>
<table>
<tr>
<td>Name :</td>
<td><input type="text" name="" validate="required min(5)"></td>
</tr>
<tr>
<td>Mobile No :</td>
<td><input type="text" name="" validate="required number min(10) max(10)">
</tr>
<tr>
<td>Email :</td>
<td><input type="text" name="" validate="required email"></td>
</tr>
</table>
<input type="submit" value="submit"/>
</form>