Solved

Time & Number validations on the web.

Posted on 2001-08-23
14
264 Views
Last Modified: 2013-12-18
Hi,

I would like to know how to do Numeric & Time-date validations on a web form.

I have tried doing the following in InputValidation-

@if(!Istime(<filedname>);@Failure("<do something>");@success).

If I keep the Field Datatype as Text/ date-time, it doesn't work.

Same holds for numeric fields too.

how do i check whether the user has entered the field value as a valid number or a valid date.

Pls help!
Thanks.
0
Comment
Question by:puneetfred
  • 7
  • 5
  • 2
14 Comments
 
LVL 5

Expert Comment

by:snocross
ID: 6419626
For the number:

@If(@IsNumber(<fieldname>);@Success;@Failure("Please enter a valid number.)

For the time:
It looks like you did it right but left out the @ sign in @isTime.

-Snocross
0
 
LVL 5

Expert Comment

by:snocross
ID: 6419630
Also, what type is your field?  You said Text/Date-Time but it is either Text OR Date-time.
0
 

Author Comment

by:puneetfred
ID: 6419816
@isTime / @ IsNumber isn't working.

The type can be anything as long as I can do the validation. it isn't working.

just tell me which event do u do validations on when on the web. is it querySave? I am on 4.5 server.

coz I can extract date Day/month/year and then check on each but I cant remember which event.

thanks!
0
 
LVL 8

Expert Comment

by:Jean Marie Geeraerts
ID: 6420943
Problem is when you enter an invalid date in a date field, notes will generate an internal server error.
The best way is to write a javascript function to perform all necessary input validations (it's a lot quicker too, since it doesn't call on the server).

Here's a function to validate a date entered as dd/mm/yyyy. You can easily change the validation function to reflect your input format.
function checkDate(datum)
   {
   // pass the date as a text to this function in the
   // format dd/mm/yyyy
   // the function returns true if it's a valid date and
   // false if the date is not valid
   // dag = day of the month
   // maand = month number
   // jaar = 4-digit year
   // februari = variable used to determin the number of
   // days of february for the entered year
   var dag, maand, jaar, februari;
   var result;
   if (datum.length<10)
      {
      result=false;
      }
   else
      {
      dag = parseInt(datum.substr(0,2),10);
      maand = parseInt(datum.substr(3,5),10);
      jaar = parseInt(datum.substr(6,datum.length),10);
      // determin the number of days for february
      var februari=0;
      if (jaar%4!=0)
         februari=28
      else if (jaar%100!=0)
         februari=29
      else if (jaar%200==0)
         februari=29
      else
         februari=28;
      // If any part of the date is not numeric the date
      // is not valid. The parseInt function returns NaN
      // for non numeric fields. isNaN will check for this
      // value and return true if parseInt failed. This
      // check can therefore also be used to check if an
      // entry is numeric
      if (dag==0 || isNaN(dag) || isNaN(maand) || isNaN(jaar))
         {
         result=false;
         }
      else
         {
         switch (maand)
            {
            case 1 :
            case 3 :
            case 5 :
            case 7 :
            case 8 :
            case 10 :
            case 12 :
               if (dag<=31)
                  result=true
               else
                  result=false;
               break;
            case 4 :
            case 6 :
            case 9 :
            case 11 :
               if (dag<=30)
                  result=true
               else
                  result=false;
               break;
            case 2 :
               if (dag<=februari)
                  result=true
               else
                  result=false;
               break;
            default :
               result=false;
            }
         }
      }
   return result;
   }

Put this function in your JS Header and call it from another function to validate the entered date.

I usually use the following as a "Save & Close" action on web forms :

var form=document.forms[0];
if (checkForm())
   form.submit();

This will perform the checkForm JavaScript function to validate all fields to be validated. If this function returns true, the form is saved if not no action is taken. Error messages are shown in the checkForm function.
From the checkForm function I call other functions (like the above checkDate) to validate all fields on the form and generate an error message if a validation fails (using the alert JavaScript function).

All functions are put in the JS Header of the form. Also make sure to put functions that are called from other functions before the calling function.

If you need any additional help writing JavaScript-code, just let me know. (I don't know if you are familiar with JavaScript or not, if you are not I can elaborate on this).

Regards,

Jerrith
0
 
LVL 8

Expert Comment

by:Jean Marie Geeraerts
ID: 6420990
Just noticed you are using Notes R4.5
In this case you have to include the JavaScript on your form as passthrough HTML at the top of your form :

<SCRIPT LANGUAGE="JavaScript">
<!--
function checkDate(Datum)
   {
   ...

   }

function checkForm()
   {
   if (!checkDate(document.forms[0].Date.value))
      {
      alert ("Please enter a valid date!");
      return false;
      }
   else
      return true;
   }

// -->
</SCRIPT>

Now to call the script when saving the form, you need to call it from the onSubmit event of the form.
To do this add the following passthru-HTML directly behind your JavaScript functions :

</FORM>
<FORM onSubmit="if (checkForm())
   return true
else
   return false;" METHOD=post>

Try this out and let me know if you get it working.

P.S.: If you would be using R5 it would be a lot easier.
0
 

Author Comment

by:puneetfred
ID: 6421928
will try it out and thanks!
0
 

Author Comment

by:puneetfred
ID: 6421985
Jerrith,

1. Where do I pass my Date value? how do i say
Call CheckDate(<DateField>).
is it in this-->
if (!checkDate(document.forms[0].Date.value))?


2. if I am using a Submit Button, can I call the validation script from that button.

I am not so good at Javascript but I have done this before, so i should do it.

thanks again!


0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 8

Expert Comment

by:Jean Marie Geeraerts
ID: 6427857
1. Yes, that's the place.
"Date" is referring to a field called Date, just replace this with the name of the date field you want to check.
What this line actually does is, pass the text-value of the field to the function and evaluate the result. If it's false, an error message is displayed. You can also add a line to put the cursor back in the field with the error :
document.forms[0].Date.focus();
where again "Date" refers to the name of your date field.


2. Yes, you could call the script from the submit button. I don't remember if you can code JavaScript behind a button in R4.5, but you can do it in R5. That I know for sure. (I'm developing in R5 nowadays).
The way I described it, the script will automatically be called when the form is submitted (=with the onSubmit parameter of the FORM). In R5, you could put this code directly in the onSubmit-event of the form. But R4.5 is not that JavaScript minded, yet.

Let me know if you need further assistance.
0
 

Author Comment

by:puneetfred
ID: 6429669
I have tried using this but i dont know where to call the function.
What is the code for calling this check from teh submit button.

I tried using $$QuerySaveAgent too. By writing a similar code in Lotus Script but doesn't work.

0
 
LVL 8

Accepted Solution

by:
Jean Marie Geeraerts earned 100 total points
ID: 6430870
If you're using R4.5 you have to do it all with passthru-HTML.
I'll repeat the complete text here so you should be able to just copy/paste it at the top of your form and then mark it all as passthru-HTML :

</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkDate(datum)
  {
  // pass the date as a text to this function in the
  // format dd/mm/yyyy
  // the function returns true if it's a valid date and
  // false if the date is not valid
  // dag = day of the month
  // maand = month number
  // jaar = 4-digit year
  // februari = variable used to determin the number of
  // days of february for the entered year
  var dag, maand, jaar, februari;
  var result;
  if (datum.length<10)
     {
     result=false;
     }
  else
     {
     dag = parseInt(datum.substr(0,2),10);
     maand = parseInt(datum.substr(3,5),10);
     jaar = parseInt(datum.substr(6,datum.length),10);
     // determin the number of days for february
     var februari=0;
     if (jaar%4!=0)
        februari=28
     else if (jaar%100!=0)
        februari=29
     else if (jaar%200==0)
        februari=29
     else
        februari=28;
     // If any part of the date is not numeric the date
     // is not valid. The parseInt function returns NaN
     // for non numeric fields. isNaN will check for this
     // value and return true if parseInt failed. This
     // check can therefore also be used to check if an
     // entry is numeric
     if (dag==0 || isNaN(dag) || isNaN(maand) || isNaN(jaar))
        {
        result=false;
        }
     else
        {
        switch (maand)
           {
           case 1 :
           case 3 :
           case 5 :
           case 7 :
           case 8 :
           case 10 :
           case 12 :
              if (dag<=31)
                 result=true
              else
                 result=false;
              break;
           case 4 :
           case 6 :
           case 9 :
           case 11 :
              if (dag<=30)
                 result=true
              else
                 result=false;
              break;
           case 2 :
              if (dag<=februari)
                 result=true
              else
                 result=false;
              break;
           default :
              result=false;
           }
        }
     }
  return result;
  }

function checkForm()
  {
  // yourDate is the name of the field containing the date
  // to be checked
  if (!checkDate(document.forms[0].yourDate.value))
     {
     alert ("Please enter a valid date!");
     return false;
     }
  else
     return true;
  }

// -->
</SCRIPT>
<FORM onSubmit="if (checkForm())
  return true
else
  return false;" METHOD=post>


This way when you submit the form the function checkForm will be called automatically and in its turn this function will call the checkDate function.
Just make sure that you put the correct fieldname in the line
   if (!checkDate(document.forms[0].yourDate.value))

P.S.: Have you considered moving to R5 ? Web development is a lot easier in R5. You would however first have to upgrade to 4.6.5, since this is the minimum release required to upgrade to R5.
0
 

Author Comment

by:puneetfred
ID: 6432158
Jerrith,

thanks for ur patience but the last part (On Submit) is still a bit goofy. As for R5, I have my certification in R5 but cest la vie that i work on 4.5

Now, lemme explain once again.

I have a set a fields in the form (which includes a date field)

where do i put this script above or below these fields. I have tried doing it at either place but what happens is that the <FORM On Submit.. > shows on the form when I do a web preview.

so, help. it is something very simple but is evading me somehow.

thanks again.

0
 

Author Comment

by:puneetfred
ID: 6432518
okay, now i have figured out a bit. but teh function is not being called.

i put the main script before the notes fields.

towards the end i wrote the following:
<INPUT TYPE=button onClick="CheckForm()" VALUE="Submit ur Ad">

Inorder to check that the function is being called, i did an alert in the begining of the function but nothing is happening.

I am getting a runtime error though-->
Line 7, Syntax error.

0
 

Author Comment

by:puneetfred
ID: 6433889
Jerrith,

I have finaly done it thru the html code but i am getting a weird append at the end of my script

<input type= Submit value = "Submit"></FORM>

wonder why i have this..i have tried all morning and cant solve it.

will try further and say it in another question.

thanks a lot!
0
 
LVL 8

Expert Comment

by:Jean Marie Geeraerts
ID: 6435231
the </FORM> is automatically generated by domino to close the form. This is a normal formatting in HTML you always have a layout like this :
<HTML>
<HEAD>
...
</HEAD>
<BODY>
<FORM>
...
</FORM>
</BODY>
</HTML>

The <FORM>-element is used to tell your browser that this is a form.
With the </FORM> at the top, you close the form generated by domino and with the following <FORM...> you start your own form. The final closing </FORM> again is generated by domino.
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

This is an old article, please see an updated version of this article, located here: http://www.experts-exchange.com/articles/23619/Notes-8-5x-Windows-7-Notes-info-and-tips.html
Notes Document Link used by IBM Notes is a link file which aids in the sharing of links to documents in email and webpages. The posts describe the importance and steps to create a Lotus Notes NDL file in brief.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

22 Experts available now in Live!

Get 1:1 Help Now