jquery validation by class

Hello experts
I want to use the jquery validation plugin with classes.
Using the test code i gat not the valid class for the notrequired field on submit.
https://jsfiddle.net/panosms/t4hy9e9e/
Any help?
<form id="test">
<label for='field1'>Field 1</label>
<input type="text" id='field1' class="notrequired" />
    <br/>
<label for='field2'>Field 2</label>
<input type="text" id="field2" class="onlydigits required" />
<br />
<input type="submit" />

$.validator.addMethod("onlydigits", $.validator.methods.digits,'Only digits allowed');
$.validator.addMethod("required", $.validator.methods.required,'Required field');
$.validator.addClassRules("onlydigits", {onlydigits: true,notrequired:true});
$.validator.addClassRules("required", {required: true});
$.validator.addClassRules("notrequired", {required:false});

Open in new window

LVL 2
PanosAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
I readded the reference to the validation library as jsFiddle runs with HTTPS and therefore it can cause issues when unsecured referenced Javascript libraries are loaded.
I also moved the code to be included in a $().ready function.
Sample:
https://jsfiddle.net/EE_RainerJ/jw9ghw2u/

HTH
Rainer
PanosAuthor Commented:
Hi Rainer Jeschor
It is still not working.
Rainer JeschorCommented:
Can you please explain what is not working?
For me it looks OK - please see the attached screenshot:
jsFiddle Screener
Thanks
Rainer
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

PanosAuthor Commented:
Take a look at my screenshot
screen
Rainer JeschorCommented:
OK, got it.
It seems that it is not possible to adjust the validator plugin to successfully mark empty not required elements.
The only "workaround" I found is to directly add the valid class to fields which are not required:
$( ".notrequired" ).addClass("valid");

Open in new window

I have also updated the jsFiddle sample.

HTH
Rainer
PanosAuthor Commented:
Hmm... I think this will not be the best result.
I had the idea to use the element,,value attributes of the function like:
$.validator.setDefaults({
          submitHandler: function() {
                  alert("submitted!");
            }
      });

$(function(){
$.validator.addMethod("requiredfalse", function(value, element) {
      $(element).addClass('valid');
      return true});
$.validator.addMethod("onlydigits", $.validator.methods.digits,'Only digits allowed');
$.validator.addMethod("required", $.validator.methods.required,'Required field');
$.validator.addClassRules("notrequired", {requiredfalse:true});
$.validator.addClassRules("onlydigits", {onlydigits: true});
$.validator.addClassRules("required", {required: true});
$("#test").validate();
});

In this case the form submits although the required field has no value

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobOwner (Aidellio)Commented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
PanosAuthor Commented:
You can delete the question if you want.
I did sent a request because i did post a working solution ID: 40723126
I was waiting for a respond from an expert to check it because  i was not 100% sure about it but I did not get any answer.
I think It will be useful for a user and the solution from expert Rainer Jeschor too.
Thank you
PanosAuthor Commented:
I think that my solution was what i was looking for
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.