Multiple validation Regex in Laravel

0

I have a function that validates everything at Frontend level with Javascript but I also need to validate it in Backend with Laravel and Regex. The function is as follows:

$('input[type=password]').keyup(function() {
		//Captura el valor de la variable escrita en tiempo real
		var pswd = $(this).val();

		var tieneOchoCaracteres = pswd.length;
		var tieneDosLetras = (pswd.match(/[a-z]{1}/ig) || []).length >= 2;
		var tieneMayus = /[A-Z]/.test(pswd);
		var tieneDosNumeros = pswd.match(/\d\D+\d/g);
		var tieneDosNumerosConsecutivos = pswd.match(/\d{2,}/);
		// var tieneDosNumeros = (pswd.match(/\d/g) || []).length >= 2;
		var tieneDosEspeciales = pswd.match(/[%_\-&#=\$@]\w+[%_\-&#=$@]/);
		var tieneDosEspecialesConsecutivos = pswd.match(/[%_\-&#=\$@]{2,}/);
		//var tieneSimbolos = (pswd.match(/[%_\-&\#=\$@]/g) || []).length >= 2;
	    
	    //validar el tamaño mayor a 8 caracteres
	    if (tieneOchoCaracteres >= 8) {
	        $('#length').removeClass('invalid').addClass('valid');
	    } else {
	        $('#length').removeClass('valid').addClass('invalid');
	    }

	    //Validar Letras
	    //Esta expresión verifica que se haya ingresado al menos dos letra de la A a la Z (en mayúsculas) o de la A a la Z (en minúsculas)
	    if (tieneDosLetras) {
	        $('#letras').removeClass('invalid').addClass('valid');
	    } else {
	        $('#letras').removeClass('valid').addClass('invalid');
	    }

	    //Validar Mayusculas
	    //Esta expresion se verifica para asegurarse de que se haya ingresado al menos una letra mayúscula
	    if (tieneMayus) {
	        $('#mayusculas').removeClass('invalid').addClass('valid');
	    } else {
	        $('#mayusculas').removeClass('valid').addClass('invalid');
	    }

	    //Validar Numeros
	    //Esto comprobará si hay dígitos del 0 al 9
	    if (tieneDosNumeros && !tieneDosNumerosConsecutivos) {
	        $('#numeros').removeClass('invalid').addClass('valid');
	    } else {
	        $('#numeros').removeClass('valid').addClass('invalid');
	    }

	    //Validar caracteres especiales
	    //Esto comprobará si hay caracteres especiales y NO consecutivos
	    if (tieneDosEspeciales && !tieneDosEspecialesConsecutivos) {
	        $('#simbolos').removeClass('invalid').addClass('valid');
	    } else {
	        $('#simbolos').removeClass('valid').addClass('invalid');
	    }
	    
	}).focus(function() {
	    $('#pswd_info').show();
	}).blur(function() {
	    $('#pswd_info').hide();
	});

Now the question is. How do I apply all these Regex rules at the backend level to the password only?

The only validation I have for the password is:

if (!empty($request->password)) {
        $request->validate([
        'password'      => 'alpha_num|max:15',
    ]);
        $actualizacion['password'] = bcrypt($request->password);
    }

But can I put the whole rule of regex in one as follows?

'password'      => 'alpha_num|max:15',/[a-z]{1}|\d\D+\d|[%_\-&#=\$@]\w+[%_\-&#=$@]|etc...'

I appreciate your help. Greetings!

    
asked by Zontir 07.11.2018 в 11:56
source

3 answers

0

The $ request- > validate would stay:

$request->validate([
    'password' => 'alpha_num|max:15',
    'regex:/[a-z]{1}|\d\D+\d|[%_\-=\$@]\w+[%_\-=$@]|etc...'
]);

For laravel 5.6, official documentation: ( link )

You can also try a line, like this:

$request->validate(['password'=>'alpha_num|max:15|regex:/[a-z]{1}|\d\D+\d|[%_\-=\$@]\w+[%_\-=$@]|etc...']);
    
answered by 07.11.2018 в 13:29
0

The correct way to define a validation rule of this type should be:

$request->validate([
    'password' =>
        array(
            'required',
            'regex:/[a-z]{1}/',
            'regex:/[A-Z]/',
            'regex:/\d\D+\d/',
            'regex:/[%_\-&#=\$@]\w+[%_\-&#=$@]/'
        ),
];

And so on for all regex rules you have.

    
answered by 08.11.2018 в 02:46
0

In the official documentation they do not incapie in regex, but they speak of two things, in the validation an array is accepted to validate and they specify how the regex is done, that way you can build your own multilevel regex, something like this

'password' => [ 'required', 'alpha_num', 'max:15' 'regex:/[a-z]{1}/ig) || []', 'regex:/[A-Z]/', 'regex:/\d\D+\d/g', 'regex:/\d{2,}/', 'regex:/[%_\-&#=\$@]\w+[%_\-&#=$@]/', 'regex:/[%_\-&#=\$@]{2,}/', ],

That's the way you could do it (I have not tested if those regex work as is, but with that you have an idea), you should try it little by little, anyway this has a big drawback, error messages that you will return, since you make several validations equal and therefore you have no way of saying which message should return for each one, to which I see two solutions, or do custom-validation-rules or do validation groups, Validate one thing and then another and so you go

link   link

    
answered by 08.11.2018 в 03:52