1
I would like to show the required message in each input element in addition to performing the verification in each pair of checkbox and input text, but it is only occurring in the first input I am focusing on.
<html>
<head>
<script src="/jscss/jquery-2.0.3.js" type="text/javascript"></script>
<script src="/jscss/jquery.validate.js" type="text/javascript"></script>
<script>
$(document).ready( function() {
//Método para verificar as notas com uso de expressão regular
$.validator.addMethod("verifica_media", function(value, element) {
return ((/^(((([1]{1}[0]{1})\.([0]{1}))|((([0_]{1})(\d){1}))\.(\d{1})))?$/i.test(value) && !($('.Checkconceito').is(":checked"))) || ((this.optional(element)) && ($('.Checkconceito').is(":checked"))));
}, "Por favor entre com uma nota válida ou o conceito F.");
$(".conceito").validate({
// Define as regras
debug: true,
rules:{
media:{
verifica_media: true
}
},
messages:{
media:{
required: "Digite uma média válida (valores entre 0.0 e 10.0"
}
}
});
});
</script>
<script src="/jscss/jquery.maskedinput.js" type="text/javascript"></script>
<script>
jQuery(function($){
$(".conceito").mask("99.9");
});
</script>
</head>
<body><form id="formularioContato" method="post">
<div align="center">
<ul>
<li>
<label>Nota:</label><input type="text" id="nota" name="nota" class="conceito"></span>
<input type="checkbox" id="notaf" name="notaf" value="notaf" class="Checkconceito">Nota F
<br />
</li>
<li>
<label>Nota:</label><input type="text" size="4" id="nota" name="nota" class="conceito"></span>
<input type="checkbox" id="notaf" name="notaf" value="notaf" class="Checkconceito">Nota F
<br />
</li>
</ul>
</div>
<input class="submit" type="submit" id="enviar" name="enviar" value="Enviar" />
</form>
</body>
</html>
Could make the generated HTML available?
– Gustavo Rodrigues
I can not unfortunately but the problem I believe is in Validator, he does not understand that I am in each input, he only intepreta the first that give focus.
– user5988
I tried to interpret your code and generate the HTML manually, I couldn’t, which made it impossible to test your code. Use invented data and/or separate only the part of code that involves the validator.
– Gustavo Rodrigues
I switched to a html test that generates basically the same case.
– user5988
I created a bin based on its code: http://jsbin.com/fekikinu/1/edit
– Gustavo Rodrigues
It would not be a problem with the Ids could not have two same ids
– Samir Braga
It seems that you are not using the plugin correctly: it validates
<form>
s and not<input>
s. Since the selector is wrong jQuery does not find the correct variables and the code fails.– Gustavo Rodrigues
The Validator takes the name and not the id, I had removed the id’s and still remains the same.
– user5988
@Samirbraga As for the Ids he is generating this code, only he use something like
id="notaf-1"
.– Gustavo Rodrigues
I changed the selector in the bin to select the form and no errors appeared in the console. There is still a problem: the method
verifica_media
does not check if the checkbox that is related toinput
that is being validated, but this is easy to arrange.– Gustavo Rodrigues
Ready! The latest version of the bin works for any number of elements by checking elements with the class
.conceito
. He still checks for the checkbox next to the input. That’s what he was looking for?– Gustavo Rodrigues