Affirmation Validation Regular Expression

Affirmation validation is used to check whether user input represents a positive or affirmative response. This is commonly needed in command-line interfaces, configuration files, form validations, and API endpoints where boolean-like string values need to be interpreted. The regex pattern covers industry-standard truthy values and common affirmative expressions across different cultures and contexts.

Recommended Solution

^(y|yes|yeah|yep|yup|true|t|ok|okay|sure|affirmative|1|on|enable|enabled|accept|accepted|confirm|confirmed|agree|agreed)$

Note: The regex uses the case-insensitive flag (i), so it matches regardless of capitalization.

Explanation

  • ^ - Start of the string.
  • (y|yes|yeah|yep|yup|...) - Alternation of all accepted affirmative values. Each value is separated by the pipe | operator.
  • $ - End of the string.
  • i flag - Case-insensitive matching, allowing "Yes", "YES", "yes", etc.

Supported Affirmative Values

  • Short forms: y, t
  • Standard affirmatives: yes, true, ok, okay
  • Casual expressions: yeah, yep, yup, sure
  • Formal expressions: affirmative, confirm, confirmed, accept, accepted, agree, agreed
  • Numeric/state values: 1, on, enable, enabled

Standards & Conventions

This pattern aligns with common conventions found in:

  • Unix/Linux systems: Command-line prompts often accept y/yes/true/1
  • Configuration files: YAML, TOML, and INI files use true/yes/on/1 for boolean values
  • Programming languages: Many languages treat "1", "true", "yes", "on" as truthy values when parsing strings to booleans
  • Web forms: Checkbox values and confirmation dialogs commonly use these patterns

Implementation

const affirmationRegex = /^(y|yes|yeah|yep|yup|true|t|ok|okay|sure|affirmative|1|on|enable|enabled|accept|accepted|confirm|confirmed|agree|agreed)$/i;
const isAffirmation = (input) => affirmationRegex.test(input);

Test Cases

This comprehensive test suite includes valid affirmative values in various cases, as well as negative cases to ensure proper rejection of non-affirmative inputs.

Input ValueValid
yes
Yes
YES
y
Y
true
True
TRUE
t
T
ok
OK
okay
Okay
yeah
yep
yup
sure
affirmative
1
on
On
ON
enable
enabled
accept
accepted
confirm
confirmed
agree
agreed
no
n
false
f
0
off
disable
disabled
reject
decline
negative
(empty string)
yes
yes!
maybe
yess
true1
1ok