1
How can I validate forms by Materialize?
I’m trying to use the jquery-validate
for this but does not work, I put the rules and even when a rule is invalid the field still turns green.
How can I make the field green only when it’s valid?
My Code Form:
<form id="testeMaterialize">
<div class="row">
<div class="input-field col s6">
<input type="number" id="nome" name="nome" class="validate" minlength="5" required>
<label for="nome" data-error="Preencha o campo Nome" class="active">Nome</label>
</div>
<div class="input-field col s6">
<input type="text" id="numero" name="numero" class="validate" required >
<label for="numero" data-error="Preencha o campo Numero corretamente" class="active">Numero</label>
</div>
</div>
</form>
My Javascript:
$(document).ready(function(){
validator = $("#testeMaterialize").validate({
rules: {
nome: {
required: true,
minlength: 5
},
numero: {
required: true,
minlength: 11,
maxlength: 14
}
},
messages: {
nome: {
required: "Por favor preencha o campo Nome",
minlength: "O Campo nome deve ter no minimo 5 caracteres"
},
numero: {
required: "Por favor preencha o campo Numero",
minlength: "O Campo número deve ter no mínimo 11 caracteres",
maxlength: "O Campo número deve ter no máximo 14 caracteres"
}
},
errorElement : 'div',
errorPlacement: function(error, element) {
var placement = $(element).data('error');
if (placement) {
$(placement).append(error)
} else {
error.insertAfter(element);
}
}
});
});
Do you want to validate in the Bank or just show the user side that the field is invalid? If it’s just a matter of UX and change the color pro invalido vc solves this with CSS no need JS
– hugocsl
It would be for the client. I thought I could do this with Materialize and Jquery-Validator.
– Gladson Bruno
Gladson if you want I make a simple example of how you validate only with CSS on the client’s side. But to validate in the Bank is not safe. CSS and HTML are just creating some rules for Input and styling red or green for example. If you want an example tell me.
– hugocsl
I want it Dude. You’re gonna help me a lot.
– Gladson Bruno