Creator Help

substring

Overview

This topic describes the syntax and usage of the substring function in Zoho Creator .

Description

The substring function extracts the characters from a string, between two specified indices ("from" and "to", not including "to" itself), and returns the new sub string.

Syntax

<string>.substring(<start index>, <end index>)

(or)

mid(string , start index, end index)

// this format is supported only in the free flow scripting mode of Script builder.

The substring function syntax has the following arguments.

string - The input string 

start index -  Required. The index where to start the extraction. First character is at index 0

end index  - Optional. The index where to stop the extraction. If omitted, it extracts the rest of the string

Example

text="Hello world welcome to the universe";

text.substring(6,11);       //returns "world"

text.substring(12);         //returns "welcome to the universe"

mid("Hello world welcome to the universe",6,11);                      //returns "world"

mid("Hello world welcome to the universe", 12;)                       //returns "welcome to the universe"

Phone Number Validation Example

This example explains a case of phone number validation. Lets say we have a Zoho Creator form to store phone numbers of users. We need to store the numbers in the format (xxx) xxx-xxxx. For example,

  1. Allow numbers entered in the following format to pass through unchanged:
    (123) 456-7890
  2. Convert numbers submitted in these formats to the above format:
    (123)456-7890
    123-456-7890

To achieve this, add the following function:

string Format.Format_Phone_Number (string in_phone)
{
if (input.in_phone == null)
{
//Phone number is null, return empty string
return "";
}
ph_num = input.in_phone.getAlphaNumeric();                   //removes all spaces and returns only alpha numeric characters
ph_num = ph_num.removeAllAlpha();                     //removes all letters
if (ph_num == "")
{
//Phone number trims down to empty string, return empty string
return "";
}
if (ph_num.length() != 10)
{
//length of phone number is not equal to 10, means it cannot be of format 1234567890
return "error";
}
ph_num_formatted = (((("(" + ph_num.subString(0,3)) + ") ") + ph_num.subString(3,6)) + "-") + ph_num.subString(6);                     //adds the special characters ( "(", ")" and "-" in the required places
return ph_num_formatted;
}

The function modifies any inserted phone number format to the required (xxx) xxx-xxxx format. You can call this function by adding the following script to the required form actions block:

Phone_Temp = thisapp.Format.Format_Phone_Number (input.Phone_Number);
if ((Phone_Temp == "") || (Phone_Temp == "error"))
{
alert("Please enter a 10 digit phone number");
}
else
{
if (input.Phone_Number != Phone_Temp)
{
input.Phone_Number = Phone_Temp;
}
}

This script returns the phone number entered in "Phone_Number" field in the required format.

Note

  • "Phone_Number" field is of single-line field type.

Top