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.

php Courses
php Courses
php Courses
How to Validate Input Fields in Laravel 11 Using alpha, alpha_num, and alpha_dash Rules

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

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    public function create()
    {
        return view('index');
    }

    public function store(Request $request)
    {
        $request->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

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    public function create()
    {
        return view('index');
    }

    public function store(Request $request)
    {
        $request->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

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    public function create()
    {
        return view('index');
    }

    public function store(Request $request)
    {
        $request->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.');
    }
}
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPregexinput validationalpha_rule
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.