Validate Form DataThe 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 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.
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.
where, team_member [name == input.name] - selects all the team_members
whose name is equal to the name currently entered. 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.
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.
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 .
Code Explanation 1. The count built-in function will return the number of rows fetched from Form A, with the given criteria:
2. The alert statement is executed if the count function returns 0
3. cancel submit statement will cancel the record update operation
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.
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.
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.
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.
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.
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.
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:
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 |