ali.irfan
05-14-2009, 07:31 AM
Hi
I have developed the following function for the validation of Zip code,the problem is, when user enters the valid zip code in first attempt, the background color of text box changes to green while I want that if user enters the valid zip code in first attempt then text box's background color should't change,if user enters invalid sip code then then text box's background color should change to red and then on entering valid zip code the text box's background color should change green?
Please help me how can I solve this problem I have tried a lot.
function isValidZipCode(name,msg) {
//name takes the id of text box
//msg takes the id of label where message will be displayed.
nameValue=document.getElementById(name).value;
var re = /^\d{5}([\-]\d{4})?$/;
if(nameValue == '' || nameValue == null ){
document.getElementById(msg).innerHTML='This field is Required';
document.getElementById(name).style.backgroundColor = "#fcc";
document.getElementById(name).style.borderColor = "#c00";
document.getElementById(name).focus();
return false;
}else
if(!re.exec(nameValue)){
document.getElementById(msg).innerHTML='Enter a valid zip code';
document.getElementById(name).style.backgroundColor = "#fcc";
document.getElementById(name).style.borderColor = "#c00";
document.getElementById(name).focus();
return false;
}
else{
document.getElementById(msg).innerHTML=' ';
document.getElementById(name).style.backgroundColor = "#cfc";
document.getElementById(name).style.borderColor = "#0c0";
}
return true;
}
I have developed the following function for the validation of Zip code,the problem is, when user enters the valid zip code in first attempt, the background color of text box changes to green while I want that if user enters the valid zip code in first attempt then text box's background color should't change,if user enters invalid sip code then then text box's background color should change to red and then on entering valid zip code the text box's background color should change green?
Please help me how can I solve this problem I have tried a lot.
function isValidZipCode(name,msg) {
//name takes the id of text box
//msg takes the id of label where message will be displayed.
nameValue=document.getElementById(name).value;
var re = /^\d{5}([\-]\d{4})?$/;
if(nameValue == '' || nameValue == null ){
document.getElementById(msg).innerHTML='This field is Required';
document.getElementById(name).style.backgroundColor = "#fcc";
document.getElementById(name).style.borderColor = "#c00";
document.getElementById(name).focus();
return false;
}else
if(!re.exec(nameValue)){
document.getElementById(msg).innerHTML='Enter a valid zip code';
document.getElementById(name).style.backgroundColor = "#fcc";
document.getElementById(name).style.borderColor = "#c00";
document.getElementById(name).focus();
return false;
}
else{
document.getElementById(msg).innerHTML=' ';
document.getElementById(name).style.backgroundColor = "#cfc";
document.getElementById(name).style.borderColor = "#0c0";
}
return true;
}