Creator Help

Form Actions - On Validate

Overview

An application that gives a free ride to input incorrect data is useless. By default, Zoho Creator supports validation for an adequate number of field types. You can specify the constraints for the fields you are defining while creating a form. For example, if you define a field to be of type Email, Zoho Creator checks if the data entered in the form for that field is a valid emailid, else it will display an error message to the user.

In addition to the default validation, Zoho creator allows you to write action scripts for custom validation. This script is executed when a user submits the form to the server, before the user data is stored in the database. The script runs on the server side, and it has access to all the inputs the user entered. The syntax for the user input is input.<variable_name> as the examples below indicate.

The default action of the validate script in the "on add" or "on edit" block, is to submit the data, so you have to do a cancel submit if you want to stop a form from being submitted. While using the validate script in the "on delete" block, you have to do a cancel delete to stop a form from deleting data.

You can also choose to provide proper alert message to the user while cancelling. The same form is then shown to the user to re-enter the data. 

Syntax

Form Actions - On Add - Validate: The validate script written in the Form Actions -> On add block is executed when a new form data is submitted. The Form definition of the script will be displayed in the Form Definition -> With Action block of the Script Builder, in the format given below. 

actions
{
on add
{

Submit
(
type = submit
displayname = "Submit"
on validate
{
// write validate form action to be executed when a new form data is submitted
.............

{
// specify alert message if not valid and cancel submission
alert "<Specify alert message>";
cancel submit;
}
}
)
}
}

See https://creator.zoho.com/sampleapps/script-actions/#Form:On_Validate and give different email addresses for the fields you see there and click on Submit. You will see the message as “Email Addresses do not match“. The following script is added to the On Add -> On validate section

if ( (input.Enter_your_Email != input.Confirm_your_Email) )
{
alert "Email Addresses do not match";
cancel submit ;
}

Form Actions - On Edit - Validate : The validate script in the Form Actions -> On Edit block is executed when an existing record is modified and submitted. The Form definition of the script will be displayed in the Form Definition -> With Action block of the Script Builder, in the format given below. 

actions
{
on edit
{
Update
(
type = submit
displayname = "Update"
validate
{
// write validate form action to be executed when an existing record
is modified and submitted
............
{
// specify alert message if not valid and cancel submission
alert "<Specify alert message>";
cancel submit;
}
}
)
}
}

See https://creator.zoho.com/sampleapps/script-actions/#View:On_Validate_View , click on Edit next to any record and give different email addresses for the fields you see there and click on Submit. After you click on Submit (with different data for emails), you will see the message as “Invalid Entries Found” and “Email Addresses do not match“. The below script is added to On Edit -> On Validate

if ( (input.Enter_your_Email != input.Confirm_your_Email) ) {
alert "Email Addresses do not match";
cancel submit ;
}

Form Actions - On Delete - Validate : The validate form action script in the Form Actions -> On Delete block is executed when an existing record is submitted for deletion. The Form definition of the script will be displayed in the Form Definition -> With Action block of the Script Builder, in the format given below. 

actions
{
on delete
{
on validate
{
// write validate form action to be executed when an existing record is deleted
...............
{
// specify alert message if not valid and cancel submission
alert "<Specify alert message>";
cancel delete;
}
}
}

If you try to delete any record from https://creator.zoho.com/sampleapps/script-actions/#View:On_Validate_View , you will get an error message as ”You do not have administrator privileges to delete records here”. Because that application checks for the logged in user and cancels the delete operation if he/she is not the administrator. Script below.

if ( zoho.loginuser != zoho.adminuser)
{
alert “You do not have administrator privileges to delete records here”;
cancel delete;
}

Steps to add the script

Refer the topic Using Script Builder for steps to add the script to your Form.

Examples

To validate if a field value falls within a specified range

The following script added to Form Actions -> On Add -> Validate block, will validate if the year specified in the DOB field is greater that the current year. If true, the submit action will get canceled.

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

To check for duplication of records 

The following script added to Form Actions -> On Add -> Validate block validates for duplicate records with same name. If a team member with the same name is added more than once, the submit action will get canceled.

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 this.name

Avoid the form to be answered twice by the same user

If you want your form not to be entered twice by the same user, you can add a logic in the On Add ->On Validate section of the form script to check if the user has entered data already.The code below avoids submission of the form using the same email twice. Find the sample scripts below:

if (count (myForm [Email == input.Email]) >1 )
{
alert “We have already received an answer from this email”;
cancel submit;
}

If you have shared the application with users, use the following script (this will not work perfectly for public forms)

if (count (myForm [Added_User == zoho.loginuser]) >1 )
{
alert “We have already received an answer from this account”;
cancel submit;
}

Top