0
I’m trying to search a string using Javascript inside my form, but I couldn’t find a method to do it. For example, I have my form with the "email" field and would like to search the domain typed by the user. I tried to use the indexOf()
and the includes()
, but I did not succeed, someone could give me a light on how I can solve this problem using Javascript/jQuery?
My HTML code:
<div class="container">
<div class="h1">Cadastro</div><br />
<form id="formulario" method="get">
<label for="nome"><b>Nome Completo</b></label>
<input type="text" placeholder="Insira o nome" name="nome" id="nome" required>
<label for="tel"><b>Telefone</b></label>
<input type="text" placeholder="Insira o telefone" name="telefone" id="telefone">
<label for="email"><b>E-mail</b></label>
<input type="text" placeholder="[email protected]" name="email" id="meu-email" required>
<label for="data"><b>Data de nascimento</b></label>
<input type="text" name="data" id="data">
<input type="submit" name="event" value="Enviar" class="btncadastra" />
</form><br />
</div>
<script src="cadastro.js"></script>
</body>
My Javascript code:
var form = document.getElementById('formulario')
var campo = document.getElementById('meu-email')
form.addEventListener('submit', function () {
if (campo.toString().includes("gmail.com")) {
alert('é gmail!')
} else {
alert('não é!')
}
});
Ah I understood what was wrong, thank you so much for the help.. was breaking my head to solve this problem!
– Cristian Guilherme Petry
@Cristianguilhermepetry, if this answer solved your problem, don’t forget to accept it (by clicking on the check icon in your upper left corner). Learn more on the [tour].
– Luiz Felipe
I had a question now, if I was going to use Sweetalert2 to show a more customizable dialog box I would have to encode my form to be submitted after I clicked the OK button in the dialog right? Because otherwise the dialog would be second and close quickly as the Forms Ubmit updates the page just when it sends.. would be on that line?
– Cristian Guilherme Petry