put a name in form name="form"
function soma() {
if (form.soma1.value == 14) {
console.log("soma correta");
}else{
console.log("por favor informe o valor correto da soma");
//para evitar o submit
return false;
}
}
<form name="form" action="envia.php" method="post" class="form" onSubmit="return soma();">
<h2 class="text-center" style="font-size: 24px;">teste</h2>
<p class="text-center" style="font-size: 16px; ">Basta preencher o formulário abaixo</p>
<div class="form-group" style="margin-top: 40px;">
<label>Nome*</label>
<input type="text" name="nome" class="form-control" required>
</div>
<div class="form-group">
<label>Email*</label>
<input type="text" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Celular*</label>
<input type="text" name="cel" class="form-control" required>
</div>
<div class="form-group">
<label>Cidade*</label>
<input type="text" name="cidade" class="form-control" required>
</div>
<div class="form-group">
<label>3 + 11 = ?</label>
<input type="text" name="soma1" value="" class="form-control" required>
</div>
<div class="form-group" style="display: block; text-align: center; margin-top: 30px;">
<input type="submit" class="btn-default" value="Agendar">
</div>
</form>
Another way: by element id
script if (document.getElementById("soma1").value == 14){
HTML <input type="text" id="soma1" name="soma1" value="" class="form-control" required>
function soma() {
if (document.getElementById("soma1").value == 14){
alert("soma correta");
}else{
alert("por favor informe o valor correto da soma");
return false;
}
}
<form action="envia.php" method="post" class="form" onSubmit="return soma();">
<h2 class="text-center" style="font-size: 24px;">teste</h2>
<p class="text-center" style="font-size: 16px; ">Basta preencher o formulário abaixo</p>
<div class="form-group" style="margin-top: 40px;">
<label>Nome*</label>
<input type="text" name="nome" class="form-control" required>
</div>
<div class="form-group">
<label>Email*</label>
<input type="text" name="email" class="form-control" required>
</div>
<div class="form-group">
<label>Celular*</label>
<input type="text" name="cel" class="form-control" required>
</div>
<div class="form-group">
<label>Cidade*</label>
<input type="text" name="cidade" class="form-control" required>
</div>
<div class="form-group">
<label>3 + 11 = ?</label>
<input type="text" id="soma1" name="soma1" value="" class="form-control" required>
</div>
<div class="form-group" style="display: block; text-align: center; margin-top: 30px;">
<input type="submit" class="btn-default" value="Agendar">
</div>
</form>
There is yet another way:
Since, by concept, there may be more than one form in a document, the forms are stored in arrays in the document.forms [] property. An index number inside the brackets points to one of the elements in the array. To access the first form in a document, the reference is: document.forms[0]
If your form is the first one in the document your script would look like this:
function soma() {
if (document.forms[0].soma1.value == 14){
console.log("soma correta");
}else{
console.log("por favor informe o valor correto da soma");
return false;
}
}
HTML
<form action="envia.php" method="post" class="form" onSubmit="return soma();">
...........
...........
<div class="form-group">
<label>3 + 11 = ?</label>
<input type="text" name="soma1" value="" class="form-control" required>
</div>
<div class="form-group" style="display: block; text-align: center; margin-top: 30px;">
<input type="submit" class="btn-default" value="Agendar">
</div>
</form>