I have an input which I enter email addresses and it only lets me enter emails that end in .com
. I would like to add other domains, in addition to that, for example these:
.cl
.net
.com.ar
Among others.
I leave the code I have:
function ValidaEmail(valor, campo){
var mensaje = "";
// comprobar los posibles errores
if (this.value == "") {
mensaje = "El email no puede estar vacío";
} else if (this.value.indexOf("@") < 0) {
mensaje = "El email debe contener una @";
} else if (this.value.indexOf(".com", this.value.indexOf("@")) < 0){
mensaje = "El email debe contener .com detras de la @";
}
// mostrar/resetear mensaje (el mensaje se resetea poniendolo a "")
this.setCustomValidity(mensaje);
}
//id del input del correo
var email = document.querySelector("#correo");
// cuando se cambie el valor del campo o sea incorrecto, mostrar/resetear mensaje
email.addEventListener("invalid", ValidaEmail);
email.addEventListener("input", ValidaEmail);
And this is an example:
function ValidaEmail(valor, campo){
var mensaje = "";
// comprobar los posibles errores
if (this.value == "") {
mensaje = "El email no puede estar vacío";
} else if (this.value.indexOf("@") < 0) {
mensaje = "El email debe contener una @";
} else if (this.value.indexOf(".com", this.value.indexOf("@")) < 0){
mensaje = "El email debe contener .com detras de la @";
}
// mostrar/resetear mensaje (el mensaje se resetea poniendolo a "")
this.setCustomValidity(mensaje);
}
//ID del input del email
var email = document.querySelector("#correo");
// cuando se cambie el valor del campo o sea incorrecto, mostrar/resetear mensaje
email.addEventListener("invalid", ValidaEmail);
email.addEventListener("input", ValidaEmail);
#correo{
font-family: 'Segoe UI';
border-radius: 5px;
border-style: solid;
border-width: 1px;
}
#mail{
font-family: 'Segoe UI';
}
button{
font-family: 'Segoe UI';
font-size: 14px;
border-radius: 5px;
border-style: solid;
border-width: 1px;
background-color: white;
border-color: black;
}
<form action="#" method="POST">
<label id="mail">Ingresa un Correo</label>
<br><br>
<input type="text" name="correo" id="correo" required="required">
<br><br>
<button type="submit">GUARDAR</button>
</form>
As I show in the example, it only allows entering .com
at the end of the mail
How could I add other options? That you can enter a domain other than .com
.
I'd like to try the function I have before trying another.
Good morning colleague! I would recommend you better to validate the input with a regular expression. Here is an example of a function for it: