Hi, i need some help with the validate plugin.
I have 4 fields who need to be validated as a whole: when all the fields are filled i have to concatenate them and then perform some checks on the string.
If anything's wrong i have to show one single message and add the error class to all the fields.
If the user correct the data and then the whole string is correct the error message is hidden and all the error classes are removed.
I basically have two problems:
- accessing all the fields and not only the one currently being edited
- updating the status (i.e. the presence of the error class) of all the fields in the group
i got around the first one by directly accessing the IDs of the field i need but it surely isn't the best solution, i guess there's something i can do to access the values of a group's fields "relatively".
I tried grouping the fields and the validation seems to work (the length check is just a stand-in for the real check) but i can't get how to update the class of all the elements of a group.
this is the HTML
- <form class="cmxform" id="texttests" method="get" action="foo.html">
- <h2 id="summary"></h2>
- <fieldset>
- <input id="cf_1" name="cf_1" size="3" maxlength="3" class="required codiceFiscale" /><br />
- <input id="cf_2" name="cf_2" size="3" maxlength="3" class="required codiceFiscale" /><br />
- <input id="cf_3" name="cf_3" size="5" maxlength="5" class="required codiceFiscale" /><br />
- <input id="cf_4" name="cf_4" size="5" maxlength="5" class="required codiceFiscale lastname" /><br />
- <input class="submit" type="submit" value="Submit"/>
- </fieldset>
- </form>
and this is the javascript part.
- $().ready(function() {
- jQuery.validator.addMethod("codiceFiscale",
- function(value){
- var codiceFiscaleTMP = $("#cf_1").val() + $("#cf_2").val() + $("#cf_3").val() + $("#cf_4").val();
- if (codiceFiscaleTMP.length != 16){
- return false;
- } else {
- return true;
- }
- },
- "Codice fiscale sbagliato");
-
- validation = $("#texttests").validate({
- groups: {
- codiceFiscale: "cf_1 cf_2 cf_3 cf_4"
- },
- errorPlacement: function(error, element) {
- if (element.attr("name") == "cf_1" || element.attr("name") == "cf_2" || element.attr("name") == "cf_3" || element.attr("name") == "cf_4"){
- error.insertAfter(".lastname");
- } else {
- error.insertAfter(element);
- }
- }
- });
- });
Any help would be really appreciated.
Thanks!
Marcello