Validation for string characters only with custom message in Laravel
In this blog I'm going to show you how you can add a validation on input allowed string characters only i.e. first-name field. There are various pre defined validations rules in Laravel integer, email, required, min, max, unique, etc. You can create custom validations in laravel also. I'm creating validation for allowed string characters only for a input. I've created a form and going to set validation on first name field. I will use regex for string characters validation. With below example you can set custom validation message as well as.
First create a controller by executing following command in command prompt. A controller will create in project_folder/app/Http/Controllers
php artisan make:controller TestController
After creating controller open controller file project_folder/app/Http/Controllers/TestController.php and copy the following code in it.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function myForm() {
return view('myform');
}
public function formValidate(Request $request) {
$request->validate(
[
'firstName' => 'required|regex:/^[a-zA-Z]+$/u|max:255',
],
[
'firstName.required' => 'First name is required',
'firstName.regex' => 'Only alphabets are allowed',
]
);
}
}
After that creating view file in resources/views/myform.blade.php and add following code in it
<html>
<head>
<title>My Custom Form</title>
</head>
<body>
<form class="user" action="{{route('formValidate')}}" method="POST">
{{ csrf_field() }}
<table border = '1'>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" value=""></td>
@error('firstName')
<span class='invalid-feedback' role='alert'>
<strong>{{ $message }}</strong>
</span>
@enderror
</tr>
<tr>
<td align = 'center' colspan = '2'
><input type="submit" value="SUBMIT"></td>
</tr>
</table>
</form>
</body>
</html>
After that we will create routes for our pages add following code in project_folder/routes/web.php
Route::get('/form','TestController@myForm');
Route::post('/validationform','TestController@formValidate')->name('formValidate');
After that visit on http://127.0.0.1:8000/form, enter the first name and hit submit button.
In this blog I'm going to show you how you can add a validation on input allowed string characters only i.e. first-name field. There are various pre defined validations rules in Laravel integer, email, required, min, max, unique, etc. You can create custom validations in laravel also. I'm creating validation for allowed string characters only for a input. I've created a form and going to set validation on first name field. I will use regex for string characters validation. With below example you can set custom validation message as well as.
First create a controller by executing following command in command prompt. A controller will create in project_folder/app/Http/Controllers
php artisan make:controller TestController
After creating controller open controller file project_folder/app/Http/Controllers/TestController.php and copy the following code in it.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function myForm() {
return view('myform');
}
public function formValidate(Request $request) {
$request->validate(
[
'firstName' => 'required|regex:/^[a-zA-Z]+$/u|max:255',
],
[
'firstName.required' => 'First name is required',
'firstName.regex' => 'Only alphabets are allowed',
]
);
}
}
After that creating view file in resources/views/myform.blade.php and add following code in it
<html>
<head>
<title>My Custom Form</title>
</head>
<body>
<form class="user" action="{{route('formValidate')}}" method="POST">
{{ csrf_field() }}
<table border = '1'>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" value=""></td>
@error('firstName')
<span class='invalid-feedback' role='alert'>
<strong>{{ $message }}</strong>
</span>
@enderror
</tr>
<tr>
<td align = 'center' colspan = '2'
><input type="submit" value="SUBMIT"></td>
</tr>
</table>
</form>
</body>
</html>
After that we will create routes for our pages add following code in project_folder/routes/web.php
Route::get('/form','TestController@myForm');
Route::post('/validationform','TestController@formValidate')->name('formValidate');
After that visit on http://127.0.0.1:8000/form, enter the first name and hit submit button.
Recommanded Articles
- How to create a multilevel category and subcategory in Laravel
- How to check YouTube video exist Laravel validation
- Multiple user roles authentication Laravel 8
- Deploy Laravel project from local to production server
- Make custom pagination URL in Laravel without query strings
- Web Scraping in Laravel using Goutte
- Insert values during migration run laravel
- Validation for string characters only with custom message in Laravel
- Add new columns in a table Laravel
- How to create foreign key constraints in Laravel
Latest Comments
Ashu Tosh
20 Dec 2020