Thursday, November 19, 2009

Form or Field Validation in Java script


//Function for remove white spaces from string both side starting and ending
function trim(str) {
return str.replace(/^\s+|\s+$/g,'');
};

//function end


//Function to check that pass value is empty or not
function checkFieldEmpty(textField,errorField){
var textFieldValue=trim(textField.value);
if(textField=="" || textFieldValue.length==0){
errorField.innerHTML="Field Can't Leave Empty";
errorField.style.visibility='visible';
errorField.style.border='#FF3333 solid 1px';
errorField.style.background='#FBE3E4';
errorField.style.color='#8a1f11';
textField.style.border='#FF3333 solid 1px';
//textField.focus();
return false;
}else{
errorField.style.visibility='hidden';
errorField.innerHTML="";
textField.style.border='#339900 solid 1px';
//return true; return "";
}
}

//end of funciton


//Function to check max length of string

// textField is a text box name
//max is the length value for check
//errorField is div show if error occur

function checkLength(textField,max,errorField){

var textFieldValue=trim(textField.value);


if(textFieldValue.length>max){

errorField.innerHTML="Field Lenght is More Then Size";
errorField.style.visibility='visible';
errorField.style.border='#FF3333 solid 1px';
errorField.style.background='#FBE3E4';
errorField.style.color='#8a1f11';
textField.style.border='#FF3333 solid 1px';
textField.focus();
return false;

}else{

errorField.style.visibility='hidden';
errorField.innerHTML="";
textField.style.border='#339900 solid 1px';
return "";
//return true;
}


}

//End of Function


//Function to check min length of string
// textField is a text box name
//min is the length value for check
//errorField is div show if error occur

function checkminLength(textField,min,errorField){

var textFieldValue=trim(textField.value);


if(textFieldValue.length
errorField.innerHTML="Field Lenght is Less Then Size";
errorField.style.visibility='visible';
errorField.style.border='#FF3333 solid 1px';
errorField.style.background='#FBE3E4';
errorField.style.color='#8a1f11';
textField.style.border='#FF3333 solid 1px';
textField.focus();
return false;
}else{

errorField.style.visibility='hidden';
errorField.innerHTML="";
textField.style.border='#339900 solid 1px';
return "";
//return true;
}
}

//Function End


//Function to check length of textarea of html or check is text area empty

// textareaField is a text area name

//errorField is div show if error occur

function checktextareaEmpty(textareaField,errorField){

var areaValue=trim(textareaField.value);

if(textareaField=="" && areaValue.length==0){

errorField.innerHTML="Text Area Can't Empty ";
errorField.style.visibility='visible';

//textareaField.focus();
return false;

}else{

errorField.style.visibility='hidden';
errorField.innerHTML='';
return "";
//return true;
}

}

// Function End

// Function for show any error or div box on select any option in select box
//selectionId is select box
//option is the value for which selection pass value is check
//showItem is div which will show the option check is true

function selectionShow(selectionId,option,showItem){

var selectionValue=trim(selectionId.value);
var opt=trim(option);
if(selectionValue==opt){

showItem.style.visibility='visible';

}else{

showItem.style.visibility='hidden';
}

}

// End Function

// Function for show div on radio button select
//buttonID is radio button id
//ShowItem is div to show

function selectRadioButton(buttonID,ShowItem){

var buttonValue=trim(buttonID.value);
alert(buttonValue);

if(buttonValue=="all"){

showItem.style.visibility='visible';

}else{

showItem.style.visibility='hidden';

}

}

// End of function


// Function for form submit ion validation that every require field not empty

function onSubmited(theForm){

var reason = "";


reason += checkFieldEmpty(document.getElementById('title'),document.getElementById('errorshow'));

reason += checkFieldEmpty(document.getElementById('user'),document.getElementById('errorshowid'));

if(document.getElementById('hide_singer').style.visibility == "visible"){

reason += checkFieldEmpty(document.getElementById('other_singer'),document.getElementById('errorsinger'));
}
if(document.getElementById('hide_lord').style.visibility == "visible"){

reason += checkFieldEmpty(document.getElementById('other_lord'),document.getElementById('errorlord'));
}
if(document.getElementById('hide_lang').style.visibility == "visible"){

reason += checkFieldEmpty(document.getElementById('other_language'),document.getElementById('errorlang'));
}
if(document.getElementById('hide_genure').style.visibility == "visible"){

reason += checkFieldEmpty(document.getElementById('other_genure_txt'),document.getElementById('errorgenure'));
}

//reason += checktextareaEmpty(theForm.other_tag,document.getElementById('errortag'));

if(reason !="") {
alert("Some fields need correction:\n" + reason);

}else{
theForm.submit();
}




}

//End of funciton

//Function for email validation

function chkEmail(elem, errorField){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
errorField.innerHTML="";
//errorshow.style.height='0px';
errorField.style.border='';

return "";
}else{
//alert(errorshow);

errorField.innerHTML="Please Enter Valid Email Address e.g. abc@abc.com";
errorField.style.visibility='visible';
errorField.style.border='#FF3333 solid 1px';
errorField.style.background='#FBE3E4';
errorField.style.color='#8a1f11';
textField.style.border='#FF3333 solid 1px';
textField.focus();

return false;
}
}

//End of function


http://opensourceprogrammer.blogspot.com/feeds/posts/default

No comments:

Post a Comment