Blog Detail


A Set of Amazing Laravel Validation Rules for Credit Card cover image

arrow_back A Set of Amazing Laravel Validation Rules for Credit Card

This is a Set of Amazing Laravel Validation Rules for Credit Cards that will validate that a given credit card number, expiration date, or CVC is valid.


As FormRequest


namespace App\Http\Requests;

use LVR\CreditCard\CardCvc;
use LVR\CreditCard\CardNumber;
use LVR\CreditCard\CardExpirationYear;
use LVR\CreditCard\CardExpirationMonth;
use Illuminate\Foundation\Http\FormRequest;

class CreditCardRequest extends FormRequest
     * Get the validation rules that apply to the request.
     * @return array
    public function rules()
        return [
            'card_number' => ['required', new CardNumber],
            'expiration_year' => ['required', new CardExpirationYear($this->get('expiration_month'))],
            'expiration_month' => ['required', new CardExpirationMonth($this->get('expiration_year'))],
            'cvc' => ['required', new CardCvc($this->get('card_number'))]

Card number

From request

    ['card_number' => '37873449367100'],
    ['card_number' => new LVR\CreditCard\CardNumber]


(new LVR\CreditCard\Cards\Visa)

Card Expiration

From request

// CardExpirationYear requires card expiration month
    ['expiration_year' => '2017'],
    ['expiration_year' => ['required', new LVR\CreditCard\CardExpirationYear($request->get('expiration_month'))]]

// CardExpirationMonth requires card expiration year
    ['expiration_month' => '11'],
    ['expiration_month' => ['required', new LVR\CreditCard\CardExpirationMonth($request->get('expiration_year'))]]

// CardExpirationDate requires date format
    ['expiration_date' => '02-18'],
    ['expiration_date' => ['required', new LVR\CreditCard\CardExpirationDate('my')]]



// Or static

Card CVC

From request

// CardCvc requires card number to determine allowed cvc length
    ['cvc' => '123'],
    ['cvc' => new LVR\CreditCard\CardCvc($request->get('card_number'))]



For more details, Please visit Github.

Published at : 11-11-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project