Thursday, January 7, 2010

Resolving JQuery.validate as my necessary

In the original JQuery.validate, 'required' action consider that the empty value of select (* dropdown in HTML) is just "". Value "0" considered this value not empty, so the 'required' action of JQuery.validate doesn't take it.
My necessary is the 'required' action is taking action when the value of select (* dropdown in HTML) was "0".

{Indonesian version : http://faridjauhari.wordpress.com/2009/12/31/jquery-validate-customize/}

The solution is making some changes in the 'required' action in "prototype" class of JQuery.validate. You can observed the changes below :
/***** original JQuery.validate (about line 883) *****/
required: function(value, element, param) {
       // check if dependency is met
       if ( !this.depend(param, element) )
            return "dependency-mismatch";
       switch( element.nodeName.toLowerCase() ) {
            case 'select':
                           var options = $("option:selected", element);
                           // this is the code that sould change
                           return options.length > 0 && ( element.type == "select-multiple" || ($.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0);
            case 'input':
                          if ( this.checkable(element) )
                              return this.getLength(value, element) > 0;
            default:
                          return $.trim(value).length > 0;
       }
}
/*-------- ori ----------*/

The changes :

/********* edited code ********/
required: function(value, element, param) {
     // check if dependency is met
     if ( !this.depend(param, element) )
          return "dependency-mismatch";
     switch( element.nodeName.toLowerCase() ) {
          case 'select':
                        var options = $("option:selected", element);
                        // the changes
                        return options.length > 0 && ( element.type == "select-multiple" || ($.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value).length > 0 && ($.browser.msie && !(options[0].attributes['value'].specified) ? options[0].text : options[0].value)!=0);
          case 'input':
                            if ( this.checkable(element) )
                                     return this.getLength(value, element) > 0;
          default:
                      return $.trim(value).length > 0;
      }
}
/*------- edited code --------*/

Finally My necessary is satisfied...hehehe....

Try it.....
----------- sharing in internet is the nice way to improve -------------------

No comments:

Post a Comment