Sri Lanka Mobile Number Validation with Regex in Laravel : Step-by-Step-Guide | Code Abe Academy

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.



    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
  1. Create mobile-number-validation.blade.php file
  2. Add Bootstrap CDN
  3. Add Form Container
  4. 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>
    
  5. Add form with Validation
  6. 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>
  7. Add form validation script.
  8. 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>
    
  9. Create a route.
  10. Web.php
    Route::get('/', function () {
        return view('mobile-number-validation');
    });
    
  11. Add Regex Mobile Number validation to form input.
  12. mobile-number-validation.blade.php
    pattern="^(070|071|072|074|075|076|077|078)\d{7}$" maxlength="10"
    
  13. Add database to .env file.
  14. .env
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=mobile_number_validation_db
    DB_USERNAME=root
    DB_PASSWORD=
    
  15. Craete Mobile Number Table and Model.
  16. php artisan make:model MobileNumberValidation -m
    
  17. Craete Mobile Number column to Table.
  18. mobile_number_validations
    $table->string('mobileNumber');
    
  19. Create Model.
  20. MobileNumberValidation Model
    protected $table = 'mobile_number_validations';
        protected $primarykey = 'id';
        protected $fillable = ['id','mobileNumber'];
    
  21. Migrate the Table.
  22. php artisan migrate
    
  23. Create Mobile Number Controller.
  24. php artisan make:controller MobileNumberController
    
  25. Add saveMobileNumber route.
  26. use App\Http\Controllers\MobileNumberController;
    
    Route::post('/saveMobileNumber',[MobileNumberController::class,'saveMobileNumber'])->name('saveMobileNumber');
    
  27. Add saveMobileNumber function.
  28. 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.

Post a Comment

Previous Post Next Post