JS does not work

Whats wrong with this code? It only returns an error if the value fields are not completed. It does not save the document nor return an error if the check box or text box fields are completed.

function ValidateFields()

{

/* Check if mandatory fields contains values */

f = document.forms[0];

//this code checks for there is a cost and desc field is null//

if (f.Cost[0].checked == true) {

if (f.Cost_Desc.value == “”) {

alert(“You don’t have value for Cost Descrption”) ;

f.Cost_Desc.focus();

}

}

//this code if checks if there is no cost but desc is filled up//

if (f.Cost_Desc.value != “”) {

if (f.Cost[0].checked == “”) {

alert(“You don’t have checked the Cost”)

f.Cost.focus();

}

}

if (f.username_dt.value == “” ||

f.dept.value == “” ||

f.category.value == “”

)

{

alert(“You must complete all entries marked mandatory: \n\n Then submit this form!”);

f.sub.focus();

}

else

{

f.submitbutton.disabled=true;

f.submit();

}

}

function CannotSave()

{

alert(“You do not have priviledges to modify this information!”);

}

Subject: JS does not work

I have had no problems usingvar f = document.forms[0];

You will need to add return false after each alert as ken suggested otherwise the code will continue to the end of the function so if the last validation passes then you will be submitting the document even though the previous ones may have failed.

Do you receive any javascript errors? If you do the icon in the bottom left hand corner of the status bar will change to an information icon and clicking on this will tell you the line where the error occurred although the message is rarely very helpful.

I would suggest putting an alert after each test and also in the else statement at the end to see where the code runs up to.

Subject: RE: JS does not work

Thanks Matt. I will change the code as you suggested. Currently it does not return any alerts. It just does not submit the document and shows an error in the bottom left hand of the doc with no further info.

Subject: RE: JS does not work

If you double click on the information icon in the bottom left hand corner then a dialog box will display that will give you a line number where the error occurred and may even give you a meaningful error message if your lucky.

When debugging javascript I put regular alert boxes in my code so that I can narrow down the block of code where the error occurs.

function ValidateFields()

{

/* Check if mandatory fields contains values */

f = document.forms[0];

//this code checks for there is a cost and desc field is null//

if (f.Cost[0].checked == true) {

if (f.Cost_Desc.value == “”) {

alert(“You don’t have value for Cost Descrption”) ;

f.Cost_Desc.focus();

}

}

alert(“Cost field validation successful”);

//this code if checks if there is no cost but desc is filled up//

if (f.Cost_Desc.value != “”) {

if (f.Cost[0].checked == “”) {

alert(“You don’t have checked the Cost”)

f.Cost.focus();

}

}

alert(“Cost Description validation successful”);

if (f.username_dt.value == “” ||

f.dept.value == “” ||

f.category.value == “”

)

{

alert(“You must complete all entries marked mandatory: \n\n Then submit this form!”);

f.sub.focus();

}

else

{

alert(“Got to else statement that submits form”);

f.submitbutton.disabled=true;

alert(“Button disabled. Submitting form”);

f.submit();

}

}

Subject: JS does not work

Taking a quick look I see no issues, but I don’t know if Notes likes the fact that you are using f=document.forms[0] to clean up the JS.

Try the good old long hand…

Subject: RE: JS does not work

You are not Returning false on the errors either…

try adding return false; after your alerts’

Subject: RE: JS does not work

Thanks Ken. Let me try your suggestion and will update you how it goes.

Thanks again.