確保資料品質的一種方式是為表單中的資料字段添加約束條件。約束條件幫助防止使用者輸入無效或不可能的答案。例如,在詢問一個人的收入時,您希望避免不切實際的值,例如負數或極高的值。在表單中添加資料約束條件很容易。只需按照以下步驟操作:

  1. 在您的表單中添加名為「constraint」的新欄位。
  2. 在「constraint」欄中,輸入指定答案限制的公式。

範例

讓我們考慮一個例子,我們想為一個人的收入添加約束條件。約束條件要求收入在 $0 到 $1,000,000 之間。以下是您設定約束條件的方式:

nameconstraint
Income. >= 0 & . <= 1000000

在上面的例子中,公式中的「.」指的是問題變數,代表使用者為「Income」問題輸入的值。約束條件「. >= 0 && . <= 1000000」確保輸入的收入大於或等於 0 且小於或等於 1,000,000。

硬性約束

硬性約束如果輸入值不滿足表達式,則完全阻止表單提交。調查員在輸入有效值之前無法繼續進行。

要添加硬性約束,請在 constraint 欄中輸入您的表達式。可選地在 constraint_message 中添加人類可讀的訊息:

typenamelabelconstraintconstraint_message
integerageAge of respondent. > 0 and . <= 120Age must be between 1 and 120
decimaltemperatureBody temperature (°C). >= 35 and . <= 42Temperature must be between 35°C and 42°C
textphonePhone numberregex(., '^[0-9]{10}$')Enter a 10-digit phone number

多個條件

使用 and / or 組合條件:

  . >= 0 and . <= 100
  
  . = 'yes' or . = 'no'
  

使用 regex() 進行模式驗證

regex(value, pattern) 函數對值進行正則表達式測試:

typenamelabelconstraintconstraint_message
textemailEmail addressregex(., '^[^@]+@[^@]+\.[^@]+$')Enter a valid email address
textzip_codeZIP coderegex(., '^[0-9]{5}$')Enter a 5-digit ZIP code

在約束條件中參考其他字段

使用 ${fieldname} 參考其他問題的值:

typenamelabelconstraintconstraint_message
integerend_yearEnd year. >= ${start_year}End year must be after start year
decimalloan_repaidAmount repaid. <= ${loan_amount}Cannot repay more than the loan amount

軟性警告

軟性警告(也稱為軟性約束或警告)警告調查員某個值看起來不尋常,但仍允許他們繼續進行。當某個值技術上有效但統計上不太可能時,這很有用。

rtSurvey 支援使用帶有特殊 constraint_type 方法的 constraint 欄,或透過與備注字段結合的外觀 soft 進行軟性警告。

最常見的模式是使用一個帶有 relevant 表達式的備注來標記可疑值,配合一個確認問題:

typenamelabelrelevant
integerchildrenNumber of children
notechildren_warningWarning: You entered ${children} children. Please confirm this is correct.. > 15
triggerchildren_confirmConfirm the number of children is correct${children} > 15

僅使用 constraint_message 的軟性警告

對於更簡單的軟性警告,您可以將約束條件設置為對極端值發出警告,但仍允許較大範圍:

typenamelabelconstraintconstraint_message
integerchildrenNumber of children. >= 0 and . <= 30This value seems very high. Please verify.
此頁面有幫助嗎?