Jquery validation

Hello experts.
I'm using jquery validation for a select box and i don't want to allow option values like:
-2,0  (-1 is a valid value).
Any help?
My select is CountrySub1_ID:
<script type="text/javascript">
jQuery.validator.addMethod(
  "selectNone",
  function(element) 
     {return true;
  }
);

// JavaScript Document
$(document).ready(function(){
   $("select").change(function() {
      $(this).valid();
        });
   $("#Registrationform").validate({
     rules: {
	   CountrySub1_ID: {required: true},
        },
	 messages: {
	  CountrySub1_ID: "Please select one"
	  }
    })
 });
</script>

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.

Shinesh PremrajanEngineering ManagerCommented:
Give a try on this
$("#Registrationform").addMethod("noanon", function(value) {
          return /[^0,-2]|[0-9]|-1/;
      }, 'Enter a valid value');


 $("#Registrationform").validate({
     rules: {
	   CountrySub1_ID: {required: true, noanon: true},
        },
	 messages: {
	  CountrySub1_ID: "Please select one"
	  }
    })

Open in new window

0
PanosAuthor Commented:
Hi shinuq.
I don't need the Enter a valid value.
If the option values are 0 or -2 i need the message "please select one".
If it is possible please don't delete the:
jQuery.validator.addMethod(
  "selectNone",
  function(element)
     {return true;
  }
);

in the top because i use this for other form selects.
0
Shinesh PremrajanEngineering ManagerCommented:
Pls find the code attached...
My select is CountrySub1_ID:
<script type="text/javascript">
jQuery.validator.addMethod(
  "selectNone",
  function(element) 
     {return true;
  }
);

// JavaScript Document
$(document).ready(function(){
   $("select").change(function() {
      $(this).valid();
        });

$("#Registrationform").addMethod("noanon", function(value) {
          return /0|-2/.test(value);
      }, 'Please select one');


   $("#Registrationform").validate({
     rules: {
	   CountrySub1_ID: {required: true, noanon: true},
        },
	 messages: {
	  CountrySub1_ID: "Please select one"
	  }
    })
 });
</script>

Open in new window

0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

PanosAuthor Commented:
Hi.
I have this error:
$("#Registrationform").addMethod is not a function
0
PanosAuthor Commented:
Hi again.
I changed the code to:
jQuery.validator.addMethod("noanon", function(value) {
          return /0|-2/.test(value);
      }, 'Please select three');

i have noerrors now but the form is passing the validation for values 0,-2 (and any digit)and not for value -1
0
Shinesh PremrajanEngineering ManagerCommented:
jQuery.validator.addMethod("noanon", function(value) {
          return /[^0,-2]/.test(value);
      }, 'Please select three');
0
PanosAuthor Commented:
Values like 10,20,30....are not passing
5,15,25,..... are passing

(my values are  x to 5)
0
Shinesh PremrajanEngineering ManagerCommented:
/[^0,-2]/ , this regular expression defines that the values 0 and -2 are not allowed, as you asked for. Interestingly, 10,20 20 contains 0 hence it is also rejected, i guess -21,-22..... are also not accepted.

/^[^0,-2]/, give a try in this, pls be specific to what is your actual values that are allowed.

Hope this helps
0
PanosAuthor Commented:
shinuq you are right.
You did answer to my question.It is my mistake.
The last comment is close to the solution.The only problem is that it don't let pass the value -1.
You can answer or i will make a new question.
0
Shinesh PremrajanEngineering ManagerCommented:
/^[^0,-2]|-1/, oops i missed that one
0

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
PanosAuthor Commented:
Thank you shinuq
It is working !!!
0
PanosAuthor Commented:
Thank you very much
regards
panos
0
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.