perscrew / react-native-form-validator

React native library to validate form fields
127 stars 55 forks source link

Custom Rules #39

Closed zapcriativo closed 4 years ago

zapcriativo commented 4 years ago

Hello, I'm trying to add a custom validation rule to validate CPF that is a document in Brazil, but it is not working. Does anyone know how to do?

Regex Used: / (^ \ d {3} . \ D {3} . \ D {3} \ - \ d {2} $) | (^ \ d {2} . \ D {3} \ . \ d {3} \ / \ d {4} \ - \ d {2} $) /

constructor(props) {
    super(props);
    this.deviceLocale = "ptBR"
    this.state = {
        cpf: '',
        nome: '',
        email: ''
    };
    this.messages = {
        ptBR: {
            required: 'O campo é obrigatório.',
            email: 'O campo "{0}" precisa conter um email válido.',
            cpf: 'O campo "{0}" precisa conter um CPF válido.',
        }
    };

    this.defaultRules = {
        cpf: /(^\d{3}\.\d{3}\.\d{3}\-\d{2}$)|(^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$)/,
    };
}
zapcriativo commented 4 years ago

It worked ...

I added a CPF validator, document in brazil..if you want to add the lib

To add in rules file

' { numbers: /^(((0-9) )|(((0-9) ) . email: /^(((^>()\gb\==\\.,;:\s@"▪+(\./06^<()\<\==\.,;:\s@ "] +) ) | (". + ")) @ ((\ [[0-9] {1,3} . [0-9] {1,3} . [0-9] {1 , 3} . [0-9] {1,3} ]) | (([a-zA-Z \ -0-9] + .) + [A-zA-Z] {2,}) ) $ /, required: / \ S + /, date (format = "YYYY-MM-DD", value) { const d = moment (value, format); if (d == null ||! d.isValid ()) return false; return true; }, minlength (length, value) { if (length === void (0)) { throw 'ERROR: It is not a valid length, checkout your minlength settings.'; } else if (value.length> = length) { return true; } return false; }, maxlength (length, value) { if (length === void (0)) { throw 'ERROR: It is not a valid length, checkout your maxlength settings.'; } else if (value.length> length) { return false; } return true; }, cpf (length, value) { var sum = 0; var rest; var i; var value = value.replace (/ [^ 0-9] / g, "");

    if (value == "00000000000") return false;
    if (value == "11111111111") return false;
    if (value == "22222222222") return false;
    if (value == "33333333333") return false;
    if (value == "44444444444") return false;
    if (value == "55555555555") return false;
    if (value == "66666666666") return false;
    if (value == "77777777777") return false;
    if (value == "88888888888") return false;
    if (value == "99999999999") return false;

    for (i = 1; i <= 9; i ++)
        sum = sum + parseInt (value.substring (i - 1, i)) * (11 - i);
    remainder = (sum * 10)% 11;

    if (rest == 10 || rest == 11) rest = 0;
    if (resto! = parseInt (value.substring (9, 10))) return false;

    sum = 0;
    for (i = 1; i <= 10; i ++)
        sum = sum + parseInt (value.substring (i - 1, i)) * (12 - i);
    remainder = (sum * 10)% 11;

    if (rest == 10 || rest == 11) rest = 0;
    if (remainder! = parseInt (value.substring (10, 11))) return false;
    return true;
}

'

MahdiBagheri71 commented 2 years ago

Hello,


constructor(props) {
       super(props);
       this.deviceLocale = "me"
       this.messages = {
           me: {
               required: ('The field "{0}" is mandatory.'),
               email: ("Please enter a valid E-mail address."),
               numbers: ("Please enter integer value."),
               date: ("Please enter a valid date."),
               int: ("Please enter a integer value."),
           }
       };
       this.rules['int'] =  /^[\-\+\ ]?\d+$/;
   }