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.
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.');
}
}
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.');
}
}
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:
You might also like:
In this article, we will show how to check whether the email already exists or not in laravel. Also, we will check...
Aug-07-2020
In this tutorial we will see how to generate pdf and send email in laravel 8. For generating PDF file we will use l...
Dec-20-2021
In this article, we will see laravel/framework[v10.0.0, ..., v10.0.3] require composer-runtime-api ^2.2 error fixed...
Mar-07-2023
In this article, we'll install the material theme in angular 17. Material Theme brings a polished design and us...
Mar-29-2024