Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript validation

Posted on 2006-07-03
10
Medium Priority
?
315 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
You’ve got a lotus Domino web server, and you have been told that “leverage browser caching” is a must do. This means that we have to tell the browser everywhere in the web to use cache. In other words, we set (and send) an expiration date in the HT…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

926 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