How to Validate Input Fields in Laravel 11 Using alpha, alpha_num, and alpha_dash Rules
This guide explains how to use Laravel 11's alpha, alpha_num, and alpha_dash validation rules to restrict input fields to specific characters, and provides complete code examples for each rule in a controller.
In Laravel 11 you can restrict input fields to specific characters by using built‑in validation rules such as alpha , alpha_num and alpha_dash . The alpha_num rule permits any Unicode letters, marks and numbers, while alpha allows only letters and marks. The alpha_dash rule extends alpha_num by also allowing the ASCII hyphen (-) and underscore (_).
alpha_num
The rule requires the field to contain only Unicode alphanumeric characters, which include:
\p{L}: all Unicode letter characters
\p{M}: all Unicode mark characters (e.g., diacritics)
\p{N}: all Unicode number characters
Thus it accepts letters and numbers from many languages.
alpha
The rule requires only Unicode letters and marks:
\p{L}: all Unicode letter characters
\p{M}: all Unicode mark characters
Digits and other symbols are rejected.
alpha_dash
This rule permits Unicode letters, marks, numbers, plus the ASCII hyphen and underscore:
\p{L}
\p{M}
\p{N}
ASCII hyphen (-)
ASCII underscore (_)
It therefore allows letters, numbers, and the characters "-" and "_".
Example 1 – Using alpha
validate([
'name' => 'required|alpha',
'email' => 'required|email|unique:users'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User created successfully.');
}
}Example 2 – Using alpha_num
validate([
'name' => 'required|alpha_num',
'email' => 'required|email|unique:users'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User created successfully.');
}
}Example 3 – Using regex for letters only
validate([
'name' => 'required|regex:/^[a-zA-Z]+$/u',
'email' => 'required|email|unique:users'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User created successfully.');
}
}php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.