Use these functions to check that the data input by the respondents, for example date and age information, is of the correct format.
IsNumeric and IsInteger
IsNumeric( argument ) IsNumeric returns true if the argument (string) is numeric.
IsInteger( argument )IsInteger returns true; if the argument (string) is an integer.
Checking that a Response in an Open Text List Question is an Integer
If you have an Open Text List question details, you can have text boxes for "name", "title", "address", "age" etc. If we want integers only to be allowed for "age", you have to provide validation code for that specific row in the open text list question. So, if "age" has code 4 in the open text list question details, we can use this validation code:
if(!IsInteger(f("details")["4"])) //Age not integer { RaiseError(); SetQuestionErrorMessage(LangIDs.en,"Please use integers only for \"age\"."); }
You could use InRange as well if you want to check that the value answered is in a reasonable range for age
IsDateFmt and IsDate
IsDateFmt( argument,format )determines whether the provided argument (string) is a valid date according to format (string).
Within a date format, the following character sequences have special significance:
| Y | Year, four digits. |
| YY | Year, two digits. |
| YYYY | Year, four digits. |
| M | Month number, one or two digits. |
| MM | Month number, exactly two digits. |
| D | Day number, one or two digits. |
| DD | Day number, exactly two digits. |
All other characters are treated as separators.
All parts are optional. If omitted, then the system date is used to determine month and year and the number 1 is used for the day.
IsDate( day,month,year )IsDate determines whether the provided year, month and day combination (all strings) constitutes a valid Gregorian date.
The month and year arguments are optional. If they are not provided then the current month and/or year is used.
For both IsDateFmt and IsDate century interpolation for 2-digit years is handled with a break-point value of 10: values between 0-10 are interpreted as the years 2000-2010, while 11-99 is treated as 1911-1999.
IsDateFmt and IsDate return an object with the properties day, month and year if the date is valid and in the correct format, null otherwise. A general description of objects follows (go to About Objects for more information), and examples where the object returned from IsDateFmt and IsDate and the properties day, month and year are used are given.
Both IsDateFmt and IsDate can be used in conditions to check for valid dates. If the date is valid and in the correct format, an object is returned. Used in a condition this will be interpreted as true. If the date is invalid or in wrong format, null will be returned. Used in a condition this will be interpreted as false.
Note: The Date question type gives a date input box and calendar popup, and has built-in validation so that you do not need to use IsDate or IsDateFmt to validate the date.
Validating Date Format of Open Text Date Question
If you have an open text question date1 and want the respondent to answer in a particular format, you can use IsDateFmt. IsDateFmt will both check that the format is correct, and that the date is a valid date.
Figure 1 - Validating date format of an open text date question
Here is a script you can use in the validation code of such a question:
if(!IsDateFmt(f("date1").get(),"YYYY-MM-DD")) { RaiseError(); SetQuestionErrorMessage(LangIDs.en,"Invalid date. Please correct. Use the format YYYY-MM-DD"); }
Validating Date with Drop-downs for the Date Parts
You can also set up date in three questions with drop-downs: One for year, one for month and one for day. To get these on the same line, you can place three grid questions in a 3D grid question as here:
Figure 2 - Setting up the date in three questions
The 3D grid's answer list will just have one item:
Figure 3 - 3D Grid answer list
The grid's answer list will be years, months and dates. It is important that you define codes that are equal to the number of the years, months (1-12) and dates (1-31):
Figure 4 - Defining codes for years
Figure 5 - Defining codes for months
Figure 6 - Defining codes for dates
The question will then look like this:
Figure 7 - Resulting question
Here is a script you can use in the validation code of such a question, using the IsDate function with the different date parts (the grid questions) as arguments:
if(!IsDate(f("day")["1"].get(),f("month")["1"].get(),f("year")["1"].get()))
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en,"Invalid date. Please correct.");
}IsEmail
IsEmail( argument )IsEmail checks whether the argument has the format of a valid email address. It returns true if it has, false otherwise.
Note: the function does not attempt any name look-ups or address verification, it just checks that the format is valid (i.e. includes an @ character etc.).
Validation of Email Address Format
An open text question email is used to collect the respondent's email address. To check that the answer is a valid email address, use the following code in the validation code field:
if(!IsEmail(f(CurrentForm())))
{
RaiseError();
SetQuestionErrorMessage(LangIDs.en,"Please provide a valid email address.");
}
Note: In Survey Designer, users can enable the ‘Email’ property on a Text question. This removes the need for validation script to check that the answer adheres to email format.
IsNet
IsNet(quadIP,quadNet,quadMask)Determines whether an IP address belongs to an Internet network.
| Argument | Description |
quadIP |
The IP address, in quad form (X.X.X.X.). |
| quadNet | The network number, in quad form. |
| quadMask | An optional mask, in quad form, if sub netting is used. |
If no mask is provided then the number of bits that constitute the network part of the address is determined from the address class type. In this case the function returns true if the IP address' network number is identical to the supplied network number and it has a non-zero local address. Otherwise it returns false.
If a mask is provided then the function returns true if the IP address' network and subnet parts are identical to the one supplied and the host number part is non-zero. Otherwise it returns false.
You can use the RequestIP function to get the respondent's IP address (go to RequestIP for more information).
Excluding Respondents from Specific Networks
Assume you wish to exclude respondents with the network number "192.168.18.0", subnet mask "255.255.255.0" from taking a specific survey (for example because they work for the company that is running the survey).
Then you may place a condition in the beginning of the questionnaire with the following expression to screen those respondents:
IsNet(RequestIP(), "192.168.18.0", "255.255.255.0")