You are viewing revision #1 of this wiki article.
This is the latest version of this article.
In India have Aadhar number an we may need to valid it a input. So I created a validator for yii2
use yii\validators\Validator;
class TAadharNumberValidator extends Validator
{
public $regExPattern = '/^\d{4}\s\d{4}\s\d{4}$/';
public function validateAttribute($model, $attribute)
{
if (preg_match($this->regExPattern, $model->$attribute)) {
$model->addError($attribute, 'Not valid Aadhar Card Number');
}
}
}
I will not recommend this validation for professional use. For example the aadhaar number 0000 0000 0000 will be validate as true.
public $regExPattern = '/^[2-9]{1}[0-9]{3}[0-9]{4}[0-9]{4}$/';
If you use this It Will Be Work Perfect and it will be show error for this For example the aadhaar number 0000 0000 0000
Nice approach with a custom Yii2 validator,
Just one thing: the logic looks reversed, you should raise the error when the regex doesn’t match, not when it does.
Also, Aadhaar validation isn’t only about format. Adding a Verhoeff checksum check would make this truly reliable for real-world use.
Solid base overall.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.