Delete all special characters except for some

0

I have a field (input) Reply To to enter an eamil and then send a message. The problem is that by modifying the code of the front I can make this input of type "text" and insert special characters, which, being a processor by the server, throws an error 500.

I know about the replaceAll method, the issue is that I do not know how to add characters to be ignored. This is what I have

replaceAll("[^a-zA-Z0-9]", "");

And from what I've read on sites like This removes all special characters except letters and numbers. I would need to add to that exception list the at ( @ ) and the period (. )

Best regards.

    
asked by Lucas. D 25.05.2018 в 23:54
source

2 answers

0

I guess what you need is to validate the email, for that java has its established method, which is quite useful (I sense it because I try to do it that way too and the laughs)

The method in question is isValidEmailAddress and it is already responsible for cleaning the String that you have to enter as a parameter, if it does not exist you have to capture the exception

public static boolean isValidEmailAddress(String email) {
 boolean result = true;
 try 
 {
   InternetAddress emailAddr = new InternetAddress(email);
   emailAddr.validate();
 } 
 catch (AddressException ex) 
 {
    result = false;
 }
    return result;
 }

And if you do not want to use the methods provided by java. You can use a standardized pattern, which I add below

import java.util.regex.Matcher; import java.util.regex.Pattern;

public class Main {
public static void main(String[] args) {

    // Patrón para validar el email
    Pattern pattern = Pattern
            .compile("^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$");

    // El email a validar
    String email = "[email protected]";

    Matcher mather = pattern.matcher(email);

    if (mather.find() == true) {
        System.out.println("El email ingresado es válido.");
    } else {
        System.out.println("El email ingresado es inválido.");
    }
}
    
answered by 26.05.2018 в 00:33
0

Suppose you have the email received from an object called mail

final String email = validarString(correo.getEmail(), 100);

I use the following method to validate that if it is mail type but also that it does not exceed 100 characters in length:

public String validarString(String campo, long longitud_maxima) {

    String filtro = campo;
    Pattern patron = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}");
    Matcher mat3 = patron.matcher(campo);

    if(mat3.matches()){
        //si el correo coincide, no haga nada, pero significa que pasó la validación
    }else{ //si el corrreo está mal escrito, devolvemos un null
    return null;
        } //fin validación email

        //validamos longitud mínima
        if (campo.length() == 0) {
            return null;
        }

        //validamos si el usuario envía varios espacios en blanco
        int whitespace_counter = 0;
        for (int i = 0; i < campo.length(); i++) {
            if (Character.isWhitespace(campo.charAt(i))) {
                whitespace_counter++;
            }
        }

        if(campo.length() == whitespace_counter) {
            return null;
        }


    //validamos longitud máxima
    if(campo.length() > longitud_maxima) {
        return null;
    }
    return filtro;
}
    
answered by 26.05.2018 в 00:40