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?
 
Shinesh PremrajanConnect With a Mentor Engineering ManagerCommented:
/^[^0,-2]|-1/, oops i missed that one
0
 
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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
 
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 PremrajanConnect With a Mentor Engineering 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
 
PanosAuthor Commented:
Thank you shinuq
It is working !!!
0
 
PanosAuthor Commented:
Thank you very much
regards
panos
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.