android Problema con validar el formato de email en un edittext
Publicado por Alfonso (1 intervención) el 24/03/2020 22:22:11
soy bastante novato en java y en android y estoy intentando que en un editext que tienen que introducir un email, al ejecutar un onClick sobre un boton me compruebe que el formato de email es el correcto, ya he hecho un if para comprobar que los diferentes campos no están vacíos pero con el de el email no doy con la tecla. os adjunto el código a ver que me podéis decir.
gracias anticipadas.
Es mi primera pregunta y no se si está bien formulada, ya me direis.
introducir el código aquí
gracias anticipadas.
Es mi primera pregunta y no se si está bien formulada, ya me direis.
introducir el código aquí
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
public void onClick(View v) {
if (validarVacios()) {
if (esEmailValido(compruebaemail)) {
// Gets the data repository in write mode
SQLiteDatabase db = Helper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(Estructura_BBDD.NOMBRE_COLUMNA1, txFecVisita.getText().toString());
values.put(Estructura_BBDD.NOMBRE_COLUMNA2, txTipoCliente.getText().toString());
values.put(Estructura_BBDD.NOMBRE_COLUMNA3, txNombre.getText().toString());
values.put(Estructura_BBDD.NOMBRE_COLUMNA4, txApellidos.getText().toString());
values.put(Estructura_BBDD.NOMBRE_COLUMNA5, txEmail.getText().toString());
values.put(Estructura_BBDD.NOMBRE_COLUMNA6, txTelefono.getText().toString());
values.put(Estructura_BBDD.NOMBRE_COLUMNA7, txFecNac.getText().toString());
values.put(Estructura_BBDD.NOMBRE_COLUMNA8, txCentroTrabajo.getText().toString());
// Insert the new row, returning the primary key value of the new row
long newRowId = db.insert(Estructura_BBDD.TABLE_NAME, null, values);
Toast.makeText(getApplicationContext(), "!!! Gracias " + txNombre.getText().toString() +
" por tu visita a Bosque Suspendido !!!", Toast.LENGTH_LONG).show();
// DEJAR LIMPIOS LOS CAMPOS.
txFecNac.setText("");
txNombre.setText("");
txApellidos.setText("");
txEmail.setText("");
txTelefono.setText("");
txFecNac.setText("");
txCentroTrabajo.setText("");
txTipoCliente.setText("");
}
}//fin If
}
});
//Validación de que no hay campos vacios y formato email
private boolean validarVacios(){
boolean vacios = true;
String c1 = txFecVisita.getText().toString();
String c2 = txNombre.getText().toString();
String c3 = txApellidos.getText().toString();
String c4 = txEmail.getText().toString();
String c5 = txTelefono.getText().toString();
String c6 = txFecNac.getText().toString();
if (c1.isEmpty()){
txFecVisita.setError("Este campo no puede quedar vacío");
vacios=false;
}
if (c2.isEmpty()){
txNombre.setError("Este campo no puede quedar vacío");
vacios=false;
}
if (c3.isEmpty()){
txApellidos.setError("Este campo no puede quedar vacío");
vacios=false;
}
if (c4.isEmpty()){
txEmail.setError("Este campo no puede quedar vacío");
vacios=false;
}
if (c5.isEmpty()){
txTelefono.setError("Este campo no puede quedar vacío");
vacios=false;
}
if (c6.isEmpty()){
txFecNac.setError("Este campo no puede quedar vacío");
vacios=false;
}
return vacios;
}
private boolean esEmailValido(String email) {
boolean isValid ;//= true;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}else{
txEmail.setError("Este campo no TIENE FORMATO DE EMAIL");
isValid = false;
}
return isValid;
}
}
Valora esta pregunta


0