panCardValidator static method

String? panCardValidator(
  1. String? value, [
  2. String? errorMessage
])

panCardValidator to validate PAN card number

validator: (value) => SimpleValidations.panCardValidator(value, [errorMessage]),

Implementation

static String? panCardValidator(String? value, [String? errorMessage]) {
  if (value == null || value.isEmpty) {
    return errorMessage ?? 'Required';
  }

  if (value.length != 10) {
    return errorMessage ?? 'Please enter a valid PAN number';
  }

  final String firstFive = value.substring(0, 5);
  final String digits = value.substring(5, 9);
  final String lastChar = value.substring(9);

  if (!firstFive.contains(CustomRegEx.panRegex[0])) {
    return errorMessage ?? 'Please enter a valid PAN number';
  }

  if (!digits.contains(CustomRegEx.panRegex[1])) {
    return errorMessage ?? 'Please enter a valid PAN number';
  }

  if (!lastChar.contains(CustomRegEx.panRegex[0])) {
    return errorMessage ?? 'Please enter a valid PAN number';
  }

  if (value[3] != 'P') {
    return errorMessage ?? 'Please enter a valid PAN number';
  }

  return null;
}