what about handling CPF if zero at left?
sibelius opened this issue · 5 comments
sibelius commented
sibelius commented
my current workaround
import { isCPF } from 'brazilian-values';
/**
* Pattern to match formatted CPF (999.999.999-99) or 11 numbers.
*/
export const CPF_PATTERN = /^(\d{11}|\d{3}\.\d{3}\.\d{3}\-\d{2})$/;
const NonNumeric = /\D/g;
const mapToNumeric = (
value: string,
): string => value.replace(NonNumeric, '');
const padding = '00000000000';
const leftPad = (str: string) => {
return padding.substring(0, padding.length - str.length) + str;
}
export const isSafeCPF = (value: string): boolean => {
if (!CPF_PATTERN.test(value)) {
const onlyNumeric = mapToNumeric(value);
const leftPadCpf = leftPad(onlyNumeric);
return isCPF(leftPadCpf);
}
return isCPF(value);
}
mechamobau commented
@sibelius maybe this can be an issue with 4devs validator (or I'm wrong) because the verifier digit returned by validation it's 71
and not 7
sibelius commented
CPF can have less than 11 digits, so the current logic is wrong
Old CPF have less than 11 digits, my father CPF for instance
You need to left pad
sibelius commented