Skip to content

String Rules

String validation rules check the content and format of string values. All rules are Unicode-aware.

RuleParametersExampleError Message
alpha$v->rule('alpha', 'field'){field} must contain only letters
alphaNum$v->rule('alphaNum', 'field'){field} must contain only letters and numbers
ascii$v->rule('ascii', 'field'){field} must contain only ASCII characters
slug$v->rule('slug', 'field'){field} must contain only letters, numbers, dashes and underscores
containssubstring, strict?$v->rule('contains', 'field', 'text'){field} must contain {0}
regexpattern$v->rule('regex', 'field', '/pattern/'){field} is not valid
startsWithprefix, caseSensitive?$v->rule('startsWith', 'field', 'pre'){field} must start with {0}
endsWithsuffix, caseSensitive?$v->rule('endsWith', 'field', 'suf'){field} must end with {0}
uuidversion?$v->rule('uuid', 'field'){field} must be a valid UUID
passwordStrengthminScore or config[]$v->rule('passwordStrength', 'field', 6){field} must be a stronger password

Note: Parameters marked with ? are optional.

Unicode support: alpha, alphaNum accept Unicode letters. slug only allows a-z, 0-9, -, _.

Case sensitivity: contains, startsWith, endsWith are case-insensitive by default. Pass true as last param for strict matching.

Multiple values: startsWith, endsWith accept arrays: ['http://', 'https://'].

Regex security: Includes ReDoS protection - dangerous patterns throw RuntimeException.

UUID versions: 1 (time), 2 (DCE), 3 (MD5), 4 (random), 5 (SHA-1). Pass version as 2nd param or omit for any.

Accepts integer (minimum score 1-10) or configuration array:

OptionTypeDefaultDescription
minScoreint6Minimum strength score (1-10)
minLengthint8Minimum password length
requireUppercaseboolfalseMust contain A-Z
requireLowercaseboolfalseMust contain a-z
requireNumberboolfalseMust contain 0-9
requireSymbolboolfalseMust contain special char

Scoring: +1 per length tier (8+, 12+, 16+), +1.5 upper, +1.5 lower, +2 number, +2 symbol. Max 10.

// Simple
$v->rule('passwordStrength', 'password', 8);
// Full config
$v->rule('passwordStrength', 'password', [
'minScore' => 7,
'requireUppercase' => true,
'requireNumber' => true
]);

Valicomb supports two syntax styles: rule-based (map rules to fields) and field-based (map fields to rules).

use Frostybee\Valicomb\Validator;
$v = new Validator($data);
$v->rules([
'alpha' => [
['first_name'],
['last_name']
],
'alphaNum' => [
['username']
],
'ascii' => [
['product_code']
],
'slug' => [
['url_slug']
],
'contains' => [
['bio', 'developer']
],
'regex' => [
['sku', '/^[A-Z]{2}-\\d{4}$/']
],
'startsWith' => [
['website', 'https://']
],
'endsWith' => [
['work_email', '@company.com']
],
'uuid' => [
['user_id']
],
'passwordStrength' => [
['password', 6]
]
]);
$v->validate();
$v->rules([
'passwordStrength' => [
['password', [
'minScore' => 7,
'minLength' => 10,
'requireUppercase' => true,
'requireLowercase' => true,
'requireNumber' => true,
'requireSymbol' => true
]]
]
]);
use Frostybee\Valicomb\Validator;
$v = new Validator($data);
$v->mapManyFieldsToRules([
'username' => ['required', 'alphaNum', ['lengthBetween', 3, 20]],
'first_name' => ['required', 'alpha'],
'last_name' => ['required', 'alpha'],
'bio' => ['optional', ['contains', 'developer']],
'password' => ['required', ['passwordStrength', 7]],
'website' => ['optional', ['startsWith', 'https://']],
'work_email' => ['required', 'email', ['endsWith', '@company.com']]
]);
$v->validate();