jQuery.validator.addMethod

You can create a simple rule by doing something like this:

jQuery.validator.addMethod("greaterThanZero", function(value, element) {
   
return this.optional(element) || (parseFloat(value) > 0);
}, "* Amount must be greater than zero");

And then applying this like so:

$('validatorElement').validate({
    rules
: {
        amount
: { greaterThanZero : true }
   
}
});

Just change the contents of the 'addMethod' to validate your checkboxes.

link | flag
answered Oct 27 '08 at 19:56
6c5c58303cef2e97644bf95fb67cd810?s=32&d=identicon&r=PG
Mark Spangler
277 1 6
1  
What is the this.optional(element) || doing in that function? It seems like every rule has that, but I can't tell why it would be relevant for any rule except "required". –  machineghost Apr 24 '09 at 22:15
1  
Leaving it out would mean that the method would always be applied, even when the element isn't required. –  Mark Spangler Apr 27 '09 at 16:34
try-sf_s04.png

vote up 2 vote down

Thanks, it worked!

Here's the final code:

$.validator.addMethod("greaterThanZero", function(value, element) {
var the_list_array = $("#some_form .super_item:checked");
return the_list_array.length > 0;
}, "* Check at least one checkbox");
link | flag
answered Oct 28 '08 at 15:03
anonymous-user-gravatar.png
edward

vote up 0 vote down

Hi edward, I need this functionality as well, but my code isn't executing... Can you post a more complete example of you code?

I don't know where to write the $.validator.addMethod, should that be outside of the form.validate code?

I currently have:

jQuery.validator.addMethod("at_least_one_cat_selected", function(value, element) { var the_list_array = $("ul#list_all_categories li input:checked"); return the_list_array.length > 0; }, "Please specify the correct domain for your documents");

and then inside the validation call:

rules: { list_all_categories: {at_least_one_cat_selected: true} },

but that isn't working. Please note: list_all_categories is the name of the that contains all checkboxes?

link | flag
answered Sep 8 '09 at 8:31
9bc6b740ee18e2a7e0f592f49046f1d3?s=32&d=identicon&r=PG
jorre
1

vote up 0 vote down
$(document).ready(function(){
$
.validator.addMethod("uniqueUserName", function(value, element) {
      $
.ajax({
          type
: "POST",
           url
: "http://"+location.host+"/checkUser.php",
          data
: "checkUsername="+value,
          dataType
:"html",
       success
: function(msg)
       
{
         
// if the user exists, it returns a string "true"
         
if(msg == "true")
             
return false;  // already exists
         
return true;      // username is free to use
       
}
     
})
}, "Username is Already Taken");
   $
("#regFormPart1").validate({
username
: {
      required
: true,
       minlength
: 8,
       uniqueUserName
: true
       
},
 messages
: {
username
: {
      required
: "Username is required",
       minlength
: "Username must be at least 8 cheractors",
       uniqueUserName
: "This Username is taken already"
     
}
   
}
 
});
link | flag
answered Feb 18 at 15:59
2eefc936c4130e2e90cc4fb361e96360?s=32&d=identicon&r=PG
Tracy
1

vote up 0 vote down
########################
## In validation Rules #
########################
regemail
: {
    required
: true,
    email
: true,
    remote
: {
        url
: "checkmail.php"
   
}
},

#####################
## In message Rules #
#####################
regemail
: {
    required
: "Email is required",
    email
: "Invalid Format",
    remote
: ""
}

###########################
## Modify Jquery Validate #
###########################
#Search For:
var valid = response === true;
if ( valid ) {

#Change To:
var valid = response.data;
if ( !valid ) {
   valid
= true;

#Search For
} else {
   
var errors = {};

#Change To
} else {
   valid
= false;
   
var errors = {};


#Search For:
errors
[element.id] = previous.message = response || validator.defaultMessage( element, "remote" );

#Change To:
errors
[element.id] = response.data || validator.defaultMessage( element, "remote" );

#PHP Returns JSON return ($m == $m) ? false : 'Email Already Registered'

转载于:https://www.cnblogs.com/hmII/archive/2010/04/26/1721259.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值