Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when field is missing

Status
Not open for further replies.

arbo80

Programmer
Jan 5, 2006
53
US
Hi,
I have a javascript code that returns an error message when a requiered field is left empty. This part works properly. Unfortunately, when one of the fields is not visible in the form, the code doesn't work anymore. I'm new with javascript. Any help will be greatly appreciated. Below is my code:

function Verify()
{
var R_Val = null;
var themessage = "Fields empty! Cannot continue";
if (Form1.CustFullnametxt.value=="") {
R_Val = 1;
}
if (Form1.InstitutioncodeSel.value=="") {
R_Val = 1; }
if (Form1.AccountOfficerSel.value=="") {
R_Val = 1; }
if (R_Val == 1) {
//Form1.submit();
return true;
}
else {
alert(themessage);
location.href=kk;
return false;
}

}

Thanks,
AB
 
[1] The reference to form, if reference by its name, should have document parent object explicit; if reference by its id, should be retrieved by document.getElementById("formid"). This is so little to add to make thing cross browser even you're interested only to support ie.

[2] The title of the thread refers to missing field. The question asked refers to "not visible" field. They are different. There are different categories: "missing", "invisible", "disabled", "hidden". They are different concept. Except "missing", the others can safely be reference in the ways you show (with better practice [1]). So if you really meant only "not visible", the problem should not arise. It only arises when the field is not there at all, or "missing". To really guard against "missing" field, you can do this. (I keep the chain of reference everywhere for minimal editing. You can make the element reference to a variable if its reference is needed more often than a single .value.)
[tt]
function Verify()
{
var R_Val = null;
var themessage = "Fields empty! Cannot continue";
if (document.Form1.CustFullnametxt && document.Form1.CustFullnametxt.value=="") {
R_Val = 1;
}
if (document.Form1.InstitutioncodeSel && document.Form1.InstitutioncodeSel.value=="") {
R_Val = 1; }
if (document.Form1.AccountOfficerSel && document.Form1.AccountOfficerSel.value=="") {
R_Val = 1; }
if (R_Val == 1) {
//document.Form1.submit();
return true;
}
else {
alert(themessage);
location.href=kk;
return false;
}
}
[/tt]
 
Thanks for the code! it's actually what I was looking for. Sorry for the confusion!

AB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top