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.
Any help?
<form id="test">
<label for='field1'>Field 1</label>
<input type="text" id='field1' class="notrequired" />
<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

Who is Participating?

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

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:
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.

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
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
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.

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:
          submitHandler: function() {

$.validator.addMethod("requiredfalse", function(value, element) {
      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});

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

From novice to tech pro — start learning today.