Validation for string characters only with custom message in Laravel

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

Also Read: Insert values during migration run laravel
<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.

Also Read: Paypal credit card payment on website in PHP

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

Also Read: Insert values during migration run laravel
<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.

Also Read: Paypal credit card payment on website in PHP

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Ashu Tosh
Ashu Tosh
20 Dec 2020

Exactly what i need! Thanks

Add your comment

Close