This tutorial will teach you how to add Sri Lanka Mobile Number Validation with Regex in Laravel Projects. Let’s do it step by step.
- Create mobile-number-validation.blade.php file
- Add Bootstrap CDN
- Add Form Container
- Add form with Validation
- Add form validation script.
- Create a route.
- Add Regex Mobile Number validation to form input.
- Add database to .env file.
- Craete Mobile Number Table and Model.
- Craete Mobile Number column to Table.
- Create Model.
- Migrate the Table.
- Create Mobile Number Controller.
- Add saveMobileNumber route.
- Add saveMobileNumber function.
Go to your project file location, and type cmd in the address bar to open the location in the Command Prompt. Then create a new project using following command.
Create a new project
composer create-project laravel/laravel:^10.0 mobile-number-validation
mobile-number-validation.blade.php
<section class="min-vh-100 d-flex flex-column align-items-center justify-content-center py-4">
<div class="container-fluid ">
<div class="row justify-content-md-center">
<div class="col-md-10 ">
<div class="card px-5 py-3 mt-3 shadow">
<h1 class="text-info text-center mt-2 mb-4">Sri Lanka Mobile Number Validation </h1>
</div>
</div>
</div>
</div>
</section>
mobile-number-validation.blade.php
<form class="needs-validation" action="" method="POST" novalidate>
@csrf
<div class="mb-2">
<label for="mobileNumber" class="form-label">Mobile Number</label>
<input type="text" class="form-control" name="mobile" id="mobile"
placeholder="Your Mobile Number" required>
<div class="invalid-feedback">
Please Enter Valid Mobile Number.
</div>
</div>
<div class="mt-2 mb-2">
<div class="d-grid">
<button class="btn btn-success float-right">Submit</button>
</div>
</div>
</form>
mobile-number-validation.blade.php
<script>
(function() {
'use strict'
var forms = document.querySelectorAll('.needs-validation')
Array.prototype.slice.call(forms)
.forEach(function(form) {
form.addEventListener('submit', function(event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
</script>
Web.php
Route::get('/', function () {
return view('mobile-number-validation');
});
mobile-number-validation.blade.php
pattern="^(070|071|072|074|075|076|077|078)\d{7}$" maxlength="10"
.env
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=mobile_number_validation_db DB_USERNAME=root DB_PASSWORD=
php artisan make:model MobileNumberValidation -m
mobile_number_validations
$table->string('mobileNumber');
MobileNumberValidation Model
protected $table = 'mobile_number_validations';
protected $primarykey = 'id';
protected $fillable = ['id','mobileNumber'];
php artisan migrate
php artisan make:controller MobileNumberController
use App\Http\Controllers\MobileNumberController;
Route::post('/saveMobileNumber',[MobileNumberController::class,'saveMobileNumber'])->name('saveMobileNumber');
public function saveMobileNumber(Request $request)
{
$validator = Validator::make($request->all(), [
'mobileNumber' => 'required|string',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$mobileNumberValidation = new MobileNumberValidation([
'mobileNumber' => $request->input('mobileNumber'),
]);
$mobileNumberValidation->save();
return 'success';
}
Break Down of the Regex Validation Pattern
- "^" - This is the start anchor. It ensure that there have no any other characters before this pattern.
- "(070|071|072|074|075|076|077|078)" - by () it denoted by grouping. by | it say that there can be have different options. by 070, 071, 072, 074, 075, 076, 077, 078 it say that the input must start with on of these 3 digit numbers.
- "\d" - this represents any digit from 0 to 9 , {7} - this means that exactly 7 digits should come after the first 3 digits (Eg: 070, 071, 072, 074, 075, 076, 077, 078)
- "$" - This is the end anchor. It ensure that there have no any other additional characters after the pattern.