Solved

Javascript validation

Posted on 2006-07-03
10
310 Views
Last Modified: 2013-12-18
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



0
Comment
Question by:varvoura
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 17030229
.value seems to be missing:

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

Author Comment

by:varvoura
ID: 17030273
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
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 17030298
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:varvoura
ID: 17030383
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?
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 250 total points
ID: 17030410
TResult is computed for display? That particular computation will take place in the Domino server, so the field TResult has a value "" in the browser. Unless you gave the field a value using JavaScript. Can you verify that, using alert()?
0
 

Author Comment

by:varvoura
ID: 17030603
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).

0
 

Author Comment

by:varvoura
ID: 17030706
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.


0
 

Author Comment

by:varvoura
ID: 17030780
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....


0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 17030921
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.
0
 

Author Comment

by:varvoura
ID: 17035753
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)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question