I have the below javascript saved as a JS resource and attached to a form. When the js function f_Submit is executed it will throw the first alert outside of any validation but it will not enter into any of my validation statements. I cannot figure out what is wrong with this code or my settings. Any pointers would be appreciated. The button is setup as follows:
{
var f = document.forms[0];
alert(f.ProcessName.value);
if (document.forms[0].LogonID.value != "")
{
alert ("Please enter your LogOn ID");
f.LogonID.focus();
f.LogonID.select();
return false;
}
if (document.forms[0].LogonID.value=="")
{
alert ("Please enter your LogOn ID");
f.LogonID.focus();
f.LogonID.select();
return false;
}
if ( f.User_Name.value=="" )
{
alert ("Please enter your Name.");
f.User_Name.focus();
f.User_Name.select();
return false;
}
if ( f.PC_Status.value=="" )
{
alert ("Please enter the status of this request, Open for new requests.");
f.PC_Status.focus();
f.PC_Status.select();
return false;
}
f.submit();
return true;
}
This script should error our regardless based on the first to conditions, yet it fails to no matter what.
Subject: RE: Javascript Submit button won’t execute if validation
Actually, there is no need for the else statements because you are returning false in the block. Only one will ever run anyway.Also, javascript can be somewhat quirky when submitting in this way.
Probably the best way to handle it is to change your button code to simply do
@command([filesave]); @command([fileclosewindow])
and then call the function below in your onSubmit event (or move the code directly into the event if this is the only place you use it)… This should ensure that the event runs correctly every time.
function f_Submit()
{
var f = document.forms[0];
alert(f.ProcessName.value);
alert(f.LogonID.value) // always a good idea and I recommend, during testing at least, that you place an alert like this one before each conditional. Show the value before you test it… this also lets you know where you are in your code… in case it breaks. I usually place alerts with numeric counters in long js functions for this very purpose… helps narrow down where to look when there are problems.
if (f.LogonID.value != “”)
{
alert (“Please enter your LogOn ID”);
f.LogonID.focus();
f.LogonID.select();
return false;
}
if (f.LogonID.value==“”)
{
alert (“Please enter your LogOn ID”);
f.LogonID.focus();
f.LogonID.select();
return false;
}
if ( f.User_Name.value==“” )
{
alert (“Please enter your Name.”);
f.User_Name.focus();
f.User_Name.select();
return false;
}
if ( f.PC_Status.value==“” )
{
alert (“Please enter the status of this request, Open for new requests.”);
Subject: RE: Javascript Submit button won’t execute if validation
I determined that for some reason the JS could not see the LogonID which is computed when composed with a default value of remote_user and yes there is a remote_user field on the form. To make the JS see the field I added the old standby of which did not rectify the issue??
Subject: RE: Javascript Submit button won’t execute if validation
LogonID has to be an editable field for this code to work correctly. A Computed field can’t be accessed in this manner (it is not an “input” type field so you can’t set focus on it or treat it as an input item in html.
Subject: RE: Javascript Submit button won’t execute if validation
I determined that for some reason the JS could not see the LogonID which is computed when composed with a default value of remote_user and yes there is a remote_user field on the form. To make the JS see the field I added the old standby of which did not rectify the issue??