JoeyEamigh / react-native-text-recognition

MIT License
23 stars 14 forks source link

how can we get card holder name and expiry date from card scanning? #15

Open Shivani12345 opened 9 months ago

Shivani12345 commented 9 months ago

For card number recognise I am using below method. I can get card number and all response as string array but how to find name and expiry date in array response.

  const result: string[] = await TextRecognition.recognize(
        image?.path as string,
      );

  const cardNumber = findCardNumberInArray(result);
      console.log('resulllttt', result);

=======================================

const findCardNumberInArray: (arr: string[]) => string = arr => {
  let creditCardNumber = '';
  arr.forEach(e => {
    let numericValues = e.replace(/\D/g, '');
    const creditCardRegex =
      /^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/;
    if (creditCardRegex.test(numericValues)) {
      creditCardNumber = numericValues;
      return;
    }
  });
  return creditCardNumber;
};
tabbathacrouch commented 1 month ago

@Shivani12345

You could use more regex!

  const namePattern = /^[A-Z][a-zA-Z]*(?: [A-Z][a-zA-Z]*)(?: [A-Z][a-zA-Z]*)?$/; // Adjusted regex to handle middle initials
  const expirationDateRegex = /(\d{2})\/(\d{2})/;