Use field name variable in jQuery validation rules

I am new to jquery. I could get validation rules valid on my form. I use the Java framework and for some reason, when it is converted from .xhtml to .html, all id / name of the ui component has a prefix of the form that contains it. I would like to reuse these validation rules for the other two forms, but due to a prefix problem, I will need to use the js variable and pass the formId to the function. For some reason this creates a problem for me:


DOES NOT WORK

var positionTitle = "#myForm:positionTitle";

$("#" + formId).validate({

  rules:{
        positionTitle : {required:true, maxlength:50},
            ...

WORKING

$("#" + formId).validate({

  rules:{
         "#myForm:positionTitle" : {required:true, maxlength:50},
              ...
+3
source share
1 answer

After calling .validate()on the form, you will do the following:

$("[name='" + positionTitle + "']").rules("add", {required:true, maxlength:50});

... , .

rules, validate(), , , , rules.

+5

Source: https://habr.com/ru/post/1767536/


All Articles