Validate Form Data

The examples given below illustrates the most common usage of the validate script. The validate scripts are generally used to validate data submitted via Zoho Creator forms before saving it to the database.

  1. To validate if a field value falls within a specified range
  2. To check for duplication of records
  3. To restrict the number of registrations to your form
  4. To restrict registrations based on a given date
  5. To restrict entries only from registered members
  6. To restrict travel bookings based on place and travel date
  7. To restrict property bookings based on Arrival/Departure dates
  8. To restrict entries only from the admin user
  9. To allow only added user to edit his record
  10. To allow only added user to delete his record
  11. To update the value of a field based on the values in other fields
  12. To make a field required dynamically

 

1. To validate if a field value falls within a specified range

In the following sample, if the year specified in the DateofBirth field is greater than the current year, the submit action will cancel. The on add -> validate script is executed when a new record is submitted. The on edit -> validate script is executed, when an existing record is submitted after editing.

on  add
{
on validate
{
if (input.DateOfBirth.getYear() > zoho.currentdate.getYear())
{
alert "Enter valid Date of Birth";
cancel submit;
}
}
} on edit
{
on validate
{
if (input.DateOfBirth.getYear() > zoho.currentdate.getYear())
{
alert "Enter valid Date of Birth";
cancel submit;
}
}
}

2. To check for duplication of records

In the below example, if the same team member is added more than once to the Team Member form, the submit action will cancel.

on  add
{
on validate
{
if (count(team_member[name == input.name]) > 0)
{
alert "Name already exists";
cancel submit;
}
}
}

where,

team_member [name == input.name] - selects all the team_members whose name is equal to the name currently entered.
input.name - it is the value for the field "name" given by the user while submitting the form
count - count operator returns the number of team members whose name is equal to the current name in the form (this.name

3. To restrict the number of registrations to your form

The following on add - validate script, checks if the number of records in the Registration form is equal to or greater than 20. Here, the count function is used to count the number of records in the Registration form. If the condition is satisfied, displays an alert message to the user and cancels the submission, so that the record is not stored in database.

on  add
{
on validate
{
if(count(Employee)>= 20)
{
alert("No more registrations allowed");
cancel submit;
}
}
}

Also refer the sample application Course Registration, which restricts the number of registrations to a specific course, based on the seats available.

4. To restrict registrations based a given date

The following deluge code, uses the zoho.currentdate function to validate if the registration date has expired.

on  add
{
on validate
{
if (zoho.currentdate > '20-Mar-2008')
{
alert "time for registration expired"; cancel submit;
}
}
}

5. To restrict entries only from registered members

The count function in Deluge Scripting enables you to count the number of records in a table that satisfies the given criteria. Using the count function, you can check if an entry already exists in a database. Let us illustrate this with the help of an example:

Assume, you have two forms: Members and Orders.

- Members form contains the list of registered members with a valid MemberID, who are eligible to subscribe through Orders form .
- Orders form is used by registered members to submit their order. Hence, when a user submits data through Orders form, we need to validate to check if the user is already a registered member. This is achieved by writing On Add -> Validate script in Orders form , as given below:

if (count(Members[MemberID == input.MemberID])== 0)
{
   alert " Specified MemberID is not Found in Members list";
cancel submit; }

Code Explanation

1. The count built-in function will return the number of rows fetched from Form A, with the given criteria:

if (count(Form_A[MemberID == input.MemberID]) == 0)

2. The alert statement is executed if the count function returns 0

alert " MemberID is not Found";

3. cancel submit statement will cancel the record update operation

cancel submit;

6. To restrict travel bookings based on place and trip date

In the following code, the travel Booking_Form restricts entries based on the following conditions:

- Only four bookings allowed for a specific place on a day. If the number of bookings (with Place & TripDate same as the input Place & TripDate) is equal to 4, the alert message will be displayed and the record will not be submitted.

- The same person cannot book on consecutive weekends. The consecutive weekend days (Saturday and Sunday) are calculated based on the current Trip date. If the same person has booked on those dates, the alert message will be displayed and the record will not be submitted.

on  add
    {
        on validate
        {
            if (count(Booking_form[(Place == input.Place && TripDate == input.TripDate)])  ==  4)
            {
                alert "Booking full on this date. Please try some other date";
                cancel submit;
            }
            else if (input.Date_1.getDayOfWeek()  >=  6)
            {
                previous_weekend1 = (input.TripDate  -  '1W:0D:0H:0M:0S');
                previous_weekend2 = (input.TripDate  -  '0W:6D:0H:0M:0S');
                if ((count(Booking_form[(Name == input.Name && TripDate == previous_weekend1)])  !=  0)  ||
(count(Booking_form[(Name == input.Name && TripDate == previous_weekend2)]) != 0))
{ alert "You cannot book consecutive weekends"; cancel submit; } } } }

 

7. To restrict property bookings based on Arrival/Departure dates

In the following code, the Property_Bookings form restricts bookings based on the Arrival and Departure dates for a specific property. For instance, if a booking is made for property named C54 between 1st and 9th Jan, the form validation should check if there is no other booking for property C54 between 1st and 9th Jan.

on  add
    {
        on validate
        {
           if (count(Property_Booking[((Arrival <= input.Departure && Departure >= input.Arrival) 
&& Property == Sinput.Property)]) > 0)
{
alert "Booking---DENIED --- property already booked for the specific date ";
cancel submit;
} } }

8. To restrict entries only from the admin user

In the following code, Added_User stores the name of the user who added the current record and zoho.adminuser returns the name of the admin user. If the added user name is not equal to the admin user name, the record will not be added.

			
on  add
    {
        on validate
        {
            if (input.Added_User  !=  zoho.adminuser)
            {
                alert "You are not authorized to add records";
                cancel submit;
            }
        }
    }

Note IconNote:

Zoho Creator automatically tracks details about when and by whom a record was added or modified using the fields Added User, Added Time, Modified User and Modified Time. This information will be displayed in the view, if the columns are selected from Column Properties option of the View tab in Edit mode.


9. To allow only added user to modify his record

Zoho Creator updates each record submission with the name of the login user who added the record and the time when it was added. The following on edit -> validate script is executed when a record is edited. If the added user name is not the same as the login username, the record is not allowed for editing.

on  edit
{
on validate
{
if (input.Added_User != zoho.loginuser)
{
alert "You can delete only your records";
cancel delete;
}
}
}

10. To allow only adder user to delete his record

Zoho Creator updates each record submission with the name of the login user who added the record and the time when it was added. Refer FAQ - Views, for more information. The following on delete -> validate script is executed when a record is deleted. If the added user name is not the same as the login username, the record is not deleted.

on  delete
{
on validate
{
if (input.Added_User != zoho.loginuser)
{
alert "You can delete only your records";
cancel delete;
}
}
}

11. To update the value of a field based on the values entered in other fields

In the following deluge code, the value of the field PatientID is calculated based on the form values specified for the First_Name and Last_Name and the value returned by the variable zoho.currenttime.

on  add
{
on validate
{
input.PatientID = input.First_Name + " " + input.Last_Name + " " + zoho.currenttime;
}
} on edit
{
on validate
{
input.PatientID = input.First_Name + " " + input.Last_Name + " " + zoho.currenttime;
}
}

12. To make a field required dynamically

Assume you have a radio button field named Field1 in your form with values "Yes"/ "No" and you want Field2 to be required only if the value of Field1 is "Yes". To achieve the given scenario, write On add -> validate script as given in the sample below:

if ((input.field1 == "Yes") && (input.field2 == null))
   {
       alert "Enter a value for field2";
cancel submit; }

The above script checks if there is any data input into field2 when field1 value is "Yes". If there is no data, submit is cancelled and an alert message is displayed.

Related Links

Deluge Reference -> Form Actions -> Validate