Link to home
Start Free TrialLog in
Avatar of varvoura
varvoura

asked on

Javascript validation

Hi all,

I am using javascript to validate all different fields on my form. I am having no problems validating all single fields, but I am not getting good results when I try to validate a field based on the results of another field. Can anyone take PLEASE take a look at my example and tell me what I am doing wrong.

Here's my example,

function validRequired()

{
  var strErr = '';

  if (document.forms[0].OrderNo.value == "")
  {

    if (strErr == '')
    {
      document.forms[0].OrderNo.focus();
    }
    strErr += 'Please enter a value in Order No Field.\n';
   
   }
   
   {  
   
    if (document.forms[0].reported.value == "")
  {

    if (strErr == '')
    {
      document.forms[0].reported.focus();
    }
    strErr += 'Please select a value in Reported by Field.\n';
       
      }
   
   {  
        if (document.forms[0].Cost.value == "$0.00" )
  {

    if (strErr == '')
    {
      document.forms[0].Cost.focus();
    }
    strErr += 'Please enter a value in Cost Field.\n';
       
      }    
                 
{  
   
    if (document.forms[0].TResult.value == "")
        {

    if (strErr == '')
    {
      document.forms[0].TResult.focus();
    }
    strErr += 'Please select a value in  Test Result Field.\n';
       
     }  
             
 {  
   
    if (document.forms[0].testdate.value == "")
  {

    if (strErr == '')
    {
      document.forms[0].testdate.focus();
    }
    strErr += 'Please select a date in Test Date Field .\n';
       
     }  
     
                 
  if (strErr == '')
  {
    return true;
  }
  else
  {
  alert(strErr);
  return false;
  }
}
}
}
}
}

All the above is working perfectly fine.

Now I want to test the value of TResults and the value there is Fail, then I want to make sure that the "Reason" field in not blank. however if the result is not Fail, then it don't have to worry about the "Reason" field.
So I wrote the following:

{
 
if (document.forms[0].TResult.value == "Fail"&&document.forms[0].Reason =="" )
{
alert("You selected Fail as test result but did not provide a reason. Please do so in the Failure Reason field");
document.forms.Reason.focus();
return;
}

I placed this underneath the portion that tests for TResult value and it didn't work.
In fact, not matter where I placed this in the script it wouldn't work so i resorted to do this bit of validation in formula language behind the save button for now.

Thank you all in advance.
Varvoura



Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

.value seems to be missing:

if (document.forms[0].TResult.value == "Fail" &&document.forms[0].Reason.value =="" )
Avatar of varvoura
varvoura

ASKER

No, I think I mistype it here only, but I know for a fact this is not the problem.
I've even declared the two arguments as variables first and used them this way to avoid problems but still no luck
for example,
I declared
var f=document.forms[0]
var m = f.TResult.value
var n = f.Reason.value

then

if (m== "Fail" && n =="" )
alert....

Still didn't work.

I have a feeling it is its placement in the script but I can't really tell well as this is my first exposure to javascript.
But looking at the script, it should work!

Thanks
What is the field TResult? A checkbox or Dialog list? Which is translated into a <SELECT> ... </SELECT> ? A Select doesn't have a value property, but a selectedIndex.
OK, TResult is just a text field(computed for display) which has the value of combox box field.
I've done that with all my combox fields(created a computed for display fields) for them to use in validation and they are working fine on all the forms. The only problem I am having is when I try using compound validation.
Am I making sense here?
ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, this is what I have and that's what happens

function validRequired()

{
var f=document.forms[0];

var m=f.TResult;

alert(m.value)

}

When I click to save&close, I first receive "" value in the alert box.

Then and & because I have @command([viewrefreshfields] behind the save&close button, I get prompted if I want to save the document, when I click "yes" the document refreshes and I get the alert box again with the value of m.value (Fail).

Nearly there....

function validRequired()

{
var f=document.forms[0];

var m=f.TResult;                       (dspfield)
var c = f.Result;                         (combo field, tResult is computed for display for that
                                                   field)
var n = f.Reason;                      (again display field)

if(m.value=="Fail"&&n.value=="")
{
alert("Please Select the reason for this test failure.");
c.focus();
return false;
}
}

On save & close & after i am prompted whether I want to save the document, I get the alert(I think because the document refreshes due to @command([viewrefreshfields];
Then I can enter a value in the reason field and I am able to save the document.

Why then, this won't work within my other script, i need them both to work together.


OK, it worked. I combine both script and it seems to be fine now.

However,

because I am using @command([viewrefreshfields]) behind the save & close action button to refresh the document so I can get the latest field values to help with validation, I am getting "would you like to save the document" so many times which is really annyoing, I get the message"would you like to save", I click "yes" this triggers the validation, then it asks me again"would you like to save", I say "Yes", it prompt me with the validation alert, when I click OK on that the second time, it disappears.
Is there an easier way to automatically refresh documents.
All form and fields properties are set to automatically refresh....


Great you solved it!

Automatic refresh is about the worst thing you can do with a form. It is only useful for spreadsheet-like solutions. And for quick-and-dirty hacks, by someone who is too lazy to do it properly (forgive me, please). In a web solution it is a disaster, since the form wil be posted to the server over and over again. In the end, the logic gets too difficult. You'd better have a very good reason to switch on automatic refresh.

When using a browser as client, there are many intermediate states a "document" can be in:
1. QueryOpen in the server
2. open in the browser
3. posted to the server as the result of a refresh request
4. refreshed in the browser
5. posted to the server in order to be saved (QuerySave)
and maybe more.

Each state has its own language. In the browser there is JavaScript, and you cannot rely upon things that get computed in the next state (as you did with a CfD field). Try not to mix up languages and states.
Thanks for your explanation, and you are so right, I'll take this into consideration with my next project.

25 years you said to get to your knowledge level? are you sure its not 15 or 10, just trying to make it easier on myself!

Kinds Regards,
GorgeousAngel(my nickname)