Laravel - 扩展验证库

I was wondering if someone could help me out.

Im writing a small application that allows people to signup for membership.

I have a plan which is paid.

I use the in build validation library to validate the name and password, but how can i extend that to validate credit card details the same way.

For example

$rules = array(
    'cardnumber' => 'required|legitcardnum'
);

So i can use it with the inbuilt library?

Any help would be greatly appreciated.

Cheers,

You can register a custom validation rule:

Validator::extend('legitcardnum', function($attribute, $value, $parameters) {
    // Maybe you need to use preg_match($pattern, $value)
    if($value == 'cardnumber') return true;
    return false;
});

You may also use a custom message:

$messages = array(
    'cardnumber.legitcardnum' => 'Invalid card number!',
);

Then you may use it:

$rules = array(
    'cardnumber' => 'required|legitcardnum'
);

Validate it like:

$input = ...;
$rules = ...;
$validator = Validator::make($input, $rules, $messages);

You may also extend the Validator class and better to do it if you want to use that custom rule more than one place/controllers/classes in your projects. Read more here. Also check PHP's preg_match function.