Special Characters Not Allowed Validation In Laravel 9

Websolutionstuff | Dec-23-2022 | Categories : Laravel PHP

In this article, we will see special characters not allowed validation in laravel 9. Here, we will learn special character is not allowed in the textbox. Sometimes we required validation for input textbox. In the textbox special character is not allowed only allow characters and numbers are allowed.

We will create alphanumeric validation in laravel 7, laravel 8, and laravel 9. So, we will give you an example of validation in laravel 7/8/9.

So, let's see laravel 9 special characters not allowed validation and special characters not allowed regex example.

Example 1:

In this example, we will use the laravel alpha validation rule. The alpha rule only allows alphabets. So, add the following code to your file or controller.

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\User;
    
class UserController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('index');
    }
    
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    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:

In this example, we will use the laravel alpha_num validation rule. The alpha_ num rule allows alphabets and numbers. So, add the following code to your file or controller.

<?php
   
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use App\User;
   
class UserController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('index');
    }
    
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    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:

In this example, we will use the regex validation rule.

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\User;
    
class UserController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('index');
    }
    
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    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.');
    }
}

Output:

special_characters_not_allowed_validation_in_laravel_9_output

 


You might also like:

Recommended Post
Featured Post
File Upload With Progress Bar In Angular 13
File Upload With Progress Bar...

In this article, we will see the file upload with a progress bar in angular 13. In this example, we will learn how to im...

Read More

Jun-11-2022

How To Import CSV File In MySQL Using Node.js
How To Import CSV File In MySQ...

In this tutorial we will see how to import CSV file in MySQL using Node.js. Import and export CSV/EXCEL file in Nod...

Read More

Jul-30-2021

How To Get Data Between Two Dates In Laravel 9
How To Get Data Between Two Da...

In this article, we will see how to get data between two dates in laravel 9. Here we will learn how to count d...

Read More

Dec-19-2022

How To Check RAM And CPU Usage In Laravel
How To Check RAM And CPU Usage...

In this tutorial, I will show you how to check RAM and CPU usage in laravel in ubuntu OS. Many times we requir...

Read More

Jul-29-2020