Solved

Javascript validation

Posted on 2006-07-03
10
305 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
  • 6
  • 4
10 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
.value seems to be missing:

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

Author Comment

by:varvoura
Comment Utility
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
Comment Utility
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
 

Author Comment

by:varvoura
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:varvoura
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For Desktop Techs: How to retain a user's Notes configuration data when swapping out the end user's computer. (Assuming that you are not upgrading to a completely different version of Notes client) All you need to do is: 1) install Notes o…
Problem "Can you help me recover my changes?  I double-clicked the attachment, made changes, and then hit Save before closing it.  But when I try to re-open it, my changes are missing!"    Solution This solution opens the Outlook Secure Temp Fold…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now