Validate character length
The Logical/charactersLength extension checks whether a supplied value contains exactly the number of characters specified by the user. It returns true when the value length matches the expected length and false when it does not.
This is useful when a journey needs to validate fixed-length codes, short identifiers, state codes, category codes, or any field where the number of characters must match a known rule before continuing with the next validation step.
Parameters:
-
coderequired, the string value to validate against the expected character length; -
codelengthrequired, the expected number of characters. This value is inserted into a regular expression quantifier, so it should be provided as a valid positive integer such as1,2, or3;
Note: The source code uses the input names code and codelength. Although the purpose description may refer to these as value and length, the extension inputs must use the exact names from the source payload.
NLP usage
Use the extension in a journey by calling Logical/charactersLength with the execute command. Pass each value to the matching extension input using as code and as codelength.
Note: This extension is synchronous and returns a boolean value directly. It does not call external services, read files, or wait for browser events.
execute "Logical/charactersLength" using "ABC" as code and "3" as codelength returning $responseexecute "Logical/charactersLength" using "AB" as code and "3" as codelength returning $responseYou can also pass stored journey variables into the extension when the value or expected length is captured earlier in the journey.
execute "Logical/charactersLength" using "$testCode" as code and "$expectedLength" as codelength returning $responsestore value "ABC" in $testCode
store value "3" in $expectedLength
execute "Logical/charactersLength" using "$testCode" as code and "$expectedLength" as codelength returning $responseExample output when the value contains exactly the expected number of characters:
trueThis extension does not require any external resource.
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: This extension validates length using the regular expression ^.{length}$. The codelength input is not converted or validated before being inserted into the expression, so invalid values can cause a regular expression error. The check counts JavaScript string characters as interpreted by the regular expression engine and may not behave as expected for multi-line text, emoji, combining characters, or other Unicode grapheme clusters. It validates only exact character count and does not check whether the value is numeric, alphabetic, uppercase, or in any specific code format.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 25/05/2026
Resources:
This extension does not require any external resource.
// Note this extension is not a product feature of Virtuoso and is not officially supported
// Extensions use javascript which may or may not be compatible with systems under test (SUTs)
// We welcome you to use this extension or adapt it for your needs
const isCharacterCode = (value, length) => new RegExp(`^.{${length}}$`).test(value);
let value = code; // Replace this with your actual value to check
let length = codelength; // Specify the length of characters you want to check (2, 3, 4, etc.)
if (isCharacterCode(value, length)) {
return(true);
} else {
return(false);
}
Comments
0 comments
Please sign in to leave a comment.