From fe24a64442a0ce7e6740abb70906a053ec54601e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Aydo=C4=9Fdu?= Date: Thu, 12 Dec 2013 14:35:34 +0200 Subject: [PATCH] v0.7.0 release - Fixed issue about input type radio validation.(#8) --- README.md | 4 ++-- examples/index.html | 6 +++++- validetta-min.js | 6 +++--- validetta.jquery.json | 4 ++-- validetta.js | 30 ++++++++++++++++++------------ 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index bc81f4d..b8185fa 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ If you want special style, you can add a class to error message with this option ```javascript { - errorClass : 'formHata' + errorClass : 'formError' } ``` @@ -88,7 +88,7 @@ You can add a class to error message closure button. ```javascript { - errorCloseClass : 'formHataKapa' + errorCloseClass : 'formErrorClose' } ``` diff --git a/examples/index.html b/examples/index.html index bec1f29..cbdac86 100644 --- a/examples/index.html +++ b/examples/index.html @@ -92,8 +92,12 @@

Example 1

- + +
  • + + +
  • diff --git a/validetta-min.js b/validetta-min.js index 3bb7fc3..7ec72cb 100644 --- a/validetta-min.js +++ b/validetta-min.js @@ -1,5 +1,5 @@ /*! - * validetta - Client-side form validation jQuery plugin - * Version: 0.6.0 (15 November 2013) | Copyright 2013 Hasan Aydoğdu - http://www.hasanaydogdu.com + * Validetta - Client-side form validation jQuery plugin + * Version: 0.7.0 (12 December 2013) | Copyright 2013 Hasan Aydoğdu - http://www.hasanaydogdu.com */ -(function(g){var e={},a={},d=new RegExp(/(minChecked|maxChecked|minSelected|maxSelected|minLength|maxLength|equal|customReg)\[[(\w)-_]{1,15}\]/i),c=new RegExp(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/),b=new RegExp(/^[\-\+]?\d+\.?\d*$/);var f={empty:"This field is required. Please be sure to check.",email:"Your E-mail address appears to be invalid. Please be sure to check.",number:"You can enter only numbers in this field.",maxLength:"Maximum {count} characters allowed!",minLength:"Minimum {count} characters allowed!",checkbox:"This checkbox is required. Please be sure to check.",maxChecked:"Maximum {count} options allowed. Please be sure to check.",minChecked:"Please select minimum {count} options.",selectbox:"Please select an option.",maxSelected:"Maximum {count} selection allowed. Please be sure to check.",minSelected:"Minimum {count} selection allowed. Please be sure to check.",notEqual:"Fields do not match. Please be sure to check.",creditCard:"Invalid credit card number. Please be sure to check."};var h={errorClass:"validetta-error",errorCloseClass:"validetta-errorClose",ajax:{call:false,type:"GET",url:null,dataType:"html",beforeSend:g.noop,success:g.noop,fail:g.noop,complete:g.noop},realTime:false,onCompleteFunc:g.noop,customReg:{}};e=function(j,i){this.handler=false;this.options=g.extend(true,{},h,i);this.form=j;return this.events.call(this)};e.prototype.events=function(){var i=this;g(this.form).submit(function(j){a=this.querySelectorAll("[data-validetta]");return i.init.call(i,j)});if(this.options.realTime===true){g(this.form).find("[data-validetta]").not("[type=checkbox]").on("change",function(j){a=g(this);return i.init.call(i,j)});g(this.form).find("[data-validetta][type=checkbox]").on("click",function(j){a=i.form.querySelectorAll("[data-validetta][type=checkbox][name="+this.name+"]");return i.init.call(i,j)})}g(this.form).find("[type=reset]").on("click",function(){return i.reset.call(i)});g(this.form).on("click","."+this.options.errorCloseClass,function(){var j=this.parentNode;if(j){i.window.close.call(i,j)}return false})};e.prototype.init=function(r){var q=this;this.reset.call(this,a);for(var p=a.length-1;p>=0;p--){var s,m,o=[],l=[];s=a[p];m="";o=g(s).val();l=s.getAttribute("data-validetta").split(",");for(var n=l.length-1;n>=0;n--){if(l[n]==="required"){var k=s.getAttribute("type");if(k==="checkbox"&&!q.check.checkbox.checked(s)){m+=f.checkbox+"
    "}else{if(s.tagName==="SELECT"&&!q.check.selectbox.selected(o)){m+=f.selectbox+"
    "}}if((k==="text"||k==="password"||s.tagName==="TEXTAREA")&&!q.check.empty.call(q,o)){m+=f.empty+"
    "}}if(l[n]==="number"&&!q.check.number(o)){m+=f.number+"
    "}if(l[n]==="email"&&!q.check.mail(o)){m+=f.email+"
    "}if(l[n]==="creditCard"&&o!==""&&!q.check.creditCard(o)){m+=f.creditCard+"
    "}if(d.test(l[n])){var t=l[n].split(/\[|,|\]/);if(t[0]==="maxLength"&&!q.check.maxLength(o,t[1])){m+=f.maxLength.replace("{count}",t[1])+"
    "}else{if(t[0]==="minLength"&&!q.check.minLength(o,t[1])){m+=f.minLength.replace("{count}",t[1])+"
    "}else{if(t[0]==="maxChecked"&&!q.check.checkbox.maxChecked.call(q,s,t[1])){s=q.form.querySelectorAll("input[type=checkbox][data-validetta][name="+s.name+"]")[0];m+=f.maxChecked.replace("{count}",t[1])+"
    "}else{if(t[0]==="minChecked"&&!q.check.checkbox.minChecked.call(q,s,t[1])){s=q.form.querySelectorAll("input[type=checkbox][data-validetta][name="+s.name+"]")[0];m+=f.minChecked.replace("{count}",t[1])+"
    "}else{if(t[0]==="maxSelected"&&!q.check.selectbox.maxSelected(o,t[1])){m+=f.maxSelected.replace("{count}",t[1])+"
    "}else{if(t[0]==="minSelected"&&!q.check.selectbox.minSelected(o,t[1])){m+=f.minSelected.replace("{count}",t[1])+"
    "}else{if(t[0]==="equal"&&!q.check.equal.call(q,o,t[1])){m+=f.notEqual+"
    "}else{if(t[0]==="customReg"&&!q.check.customReg(o,q.options.customReg[t[1]].method)){m+=(q.options.customReg[t[1]].errorMessage||f.empty)+"
    "}}}}}}}}}}if(m!==""){q.window.open.call(q,s,m)}}if(r.type!=="submit"){return}else{if(q.handler===true){return false}else{if(q.options.ajax.call){q.ajax.call(q,arguments);return false}return q.options.onCompleteFunc(q,r)}}};e.prototype.check={empty:function(i){return(this.clear(i)==="")?false:true},mail:function(i){return((c.test(i)===false)&&i!=="")?false:true},number:function(i){return((b.test(i)===false)&&i!=="")?false:true},minLength:function(j,i){var k=j.length;return(ki)?false:true},equal:function(j,i){return(g(this.form).find("input[name="+i+"]").val()!==j)?false:true},creditCard:function(k){var j,r,o,p,l,n,m=0,q;j=new RegExp(/[^0-9]+/g);r=k.replace(j,"");q=r.length;if(q<16){return false}for(l=0;l9){n=1+(n-10)}}else{n=p}m+=n}if(m>0&&m%10===0){return true}return false},checkbox:{checked:function(i){return(!i.checked)?false:true},maxChecked:function(k,i){var j=g(this.form.querySelectorAll("input[type=checkbox][name="+k.name+"]")).filter(":checked").length;return(j>i)?false:true},minChecked:function(k,i){var j=g(this.form.querySelectorAll("input[type=checkbox][name="+k.name+"]")).filter(":checked").length;return(ji)?false:true},minSelected:function(j,i){return(j!==null&&j!==""&&j.length0){return}var o,j,p,l,i,k;o=g(q).position();j=g(q).width();p=g(q).height();l=o.top;i=document.createElement("span");i.className=this.options.errorClass;k=document.createElement("span");k.innerHTML="x";k.className=this.options.errorCloseClass;g(i).empty().css({left:o.left+j+30+"px",top:l+"px"});m.appendChild(i);i.innerHTML=n;i.appendChild(k);this.handler=true},close:function(i){i.parentNode.removeChild(i);this.handler=false}};e.prototype.reset=function(k){var l={};if(typeof k==="undefined"||(k.length>1&&k[0].getAttribute("type")!=="checkbox")){l=g(this.form).find("."+this.options.errorClass)}else{l=g(k[0].parentNode).find("."+this.options.errorClass)}for(var j=l.length-1;j>=0;j--){this.window.close.call(this,l[j])}};e.prototype.clear=function(i){return i.replace(/^\s+|\s+$/g,"")};e.prototype.ajax=function(){var l,j,k=this,i;l=g(this.form).serialize();i=this.form.getAttribute("action");j=(this.options.ajax.url)?this.options.ajax.url:i;if(!this.options.ajax.url&&(i===""||i===null)){return console.log("Form action not valid !")}g.ajax({type:k.options.ajax.type,url:j,data:l,dataType:k.options.ajax.dataType,options:k.options,beforeSend:function(){return k.options.ajax.beforeSend()}}).done(function(m){k.options.ajax.success(k,m)}).fail(function(m,n){k.options.ajax.fail(m,n)}).always(function(m){k.options.ajax.complete(m)})};g.fn.validetta=function(i){if(g.validettaLanguage){f=g.extend(true,{},f,g.validettaLanguage.messages)}return this.each(function(){new e(this,i);return this})}})(jQuery); \ No newline at end of file +(function(f){var h={},a={},d=new RegExp(/(minChecked|maxChecked|minSelected|maxSelected|minLength|maxLength|equal|customReg)\[[(\w)-_]{1,15}\]/i),c=new RegExp(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/),b=new RegExp(/^[\-\+]?\d+\.?\d*$/);var e={empty:"This field is required. Please be sure to check.",email:"Your E-mail address appears to be invalid. Please be sure to check.",number:"You can enter only numbers in this field.",maxLength:"Maximum {count} characters allowed!",minLength:"Minimum {count} characters allowed!",checkbox:"This checkbox is required. Please be sure to check.",maxChecked:"Maximum {count} options allowed. Please be sure to check.",minChecked:"Please select minimum {count} options.",selectbox:"Please select an option.",maxSelected:"Maximum {count} selection allowed. Please be sure to check.",minSelected:"Minimum {count} selection allowed. Please be sure to check.",notEqual:"Fields do not match. Please be sure to check.",creditCard:"Invalid credit card number. Please be sure to check."};var g={errorClass:"validetta-error",errorCloseClass:"validetta-errorClose",ajax:{call:false,type:"GET",url:null,dataType:"html",beforeSend:f.noop,success:f.noop,fail:f.noop,complete:f.noop},realTime:false,onCompleteFunc:f.noop,customReg:{}};h=function(j,i){this.handler=false;this.options=f.extend(true,{},g,i);this.form=j;return this.events.call(this)};h.prototype.events=function(){var i=this;f(this.form).submit(function(j){a=this.querySelectorAll("[data-validetta]");return i.init.call(i,j)});if(this.options.realTime===true){f(this.form).find("[data-validetta]").not("[type=checkbox]").on("change",function(j){a=f(this);return i.init.call(i,j)});f(this.form).find("[data-validetta][type=checkbox]").on("click",function(j){a=i.form.querySelectorAll("[data-validetta][type=checkbox][name="+this.name+"]");return i.init.call(i,j)})}f(this.form).find("[type=reset]").on("click",function(){return i.reset.call(i)});f(this.form).on("click","."+this.options.errorCloseClass,function(){var j=this.parentNode;if(j){i.window.close.call(i,j)}return false})};h.prototype.init=function(r){var q=this;this.reset.call(this,a);for(var p=a.length-1;p>=0;p--){var s,m,o=[],l=[];s=a[p];m="";o=f(s).val();l=s.getAttribute("data-validetta").split(",");for(var n=l.length-1;n>=0;n--){if(l[n]==="required"){var k=s.getAttribute("type");if(k==="checkbox"&&!q.check.checkbox.checked(s)){m+=e.checkbox+"
    "}else{if(k==="radio"&&q.check.radio.call(q,s)){m+=e.empty+"
    "}else{if(s.tagName==="SELECT"&&!q.check.selectbox.selected(o)){m+=e.selectbox+"
    "}}}if((k==="text"||k==="password"||s.tagName==="TEXTAREA")&&!q.check.empty.call(q,o)){m+=e.empty+"
    "}}if(l[n]==="number"&&!q.check.number(o)){m+=e.number+"
    "}if(l[n]==="email"&&!q.check.mail(o)){m+=e.email+"
    "}if(l[n]==="creditCard"&&o!==""&&!q.check.creditCard(o)){m+=e.creditCard+"
    "}if(d.test(l[n])){var t=l[n].split(/\[|,|\]/);if(t[0]==="maxLength"&&!q.check.maxLength(o,t[1])){m+=e.maxLength.replace("{count}",t[1])+"
    "}else{if(t[0]==="minLength"&&!q.check.minLength(o,t[1])){m+=e.minLength.replace("{count}",t[1])+"
    "}else{if(t[0]==="maxChecked"&&!q.check.checkbox.maxChecked.call(q,s,t[1])){s=q.form.querySelectorAll("input[type=checkbox][data-validetta][name="+s.name+"]")[0];m+=e.maxChecked.replace("{count}",t[1])+"
    "}else{if(t[0]==="minChecked"&&!q.check.checkbox.minChecked.call(q,s,t[1])){s=q.form.querySelectorAll("input[type=checkbox][data-validetta][name="+s.name+"]")[0];m+=e.minChecked.replace("{count}",t[1])+"
    "}else{if(t[0]==="maxSelected"&&!q.check.selectbox.maxSelected(o,t[1])){m+=e.maxSelected.replace("{count}",t[1])+"
    "}else{if(t[0]==="minSelected"&&!q.check.selectbox.minSelected(o,t[1])){m+=e.minSelected.replace("{count}",t[1])+"
    "}else{if(t[0]==="equal"&&!q.check.equal.call(q,o,t[1])){m+=e.notEqual+"
    "}else{if(t[0]==="customReg"&&!q.check.customReg(o,q.options.customReg[t[1]].method)){m+=(q.options.customReg[t[1]].errorMessage||e.empty)+"
    "}}}}}}}}}}if(m!==""){q.window.open.call(q,s,m)}}if(r.type!=="submit"){return}else{if(q.handler===true){return false}else{if(q.options.ajax.call){q.ajax.call(q,arguments);return false}return q.options.onCompleteFunc(q,r)}}};h.prototype.check={empty:function(i){return(this.clear(i)==="")?false:true},mail:function(i){return((c.test(i)===false)&&i!=="")?false:true},number:function(i){return((b.test(i)===false)&&i!=="")?false:true},minLength:function(j,i){var k=j.length;return(ki)?false:true},equal:function(j,i){return(f(this.form).find("input[name="+i+"]").val()!==j)?false:true},creditCard:function(k){var j,r,o,p,l,n,m=0,q;j=new RegExp(/[^0-9]+/g);r=k.replace(j,"");q=r.length;if(q<16){return false}for(l=0;l9){n=1+(n-10)}}else{n=p}m+=n}if(m>0&&m%10===0){return true}return false},checkbox:{checked:function(i){return(!i.checked)?false:true},maxChecked:function(k,i){var j=f(this.form.querySelectorAll("input[type=checkbox][name="+k.name+"]")).filter(":checked").length;return(j>i)?false:true},minChecked:function(k,i){var j=f(this.form.querySelectorAll("input[type=checkbox][name="+k.name+"]")).filter(":checked").length;return(ji)?false:true},minSelected:function(j,i){return(j!==null&&j!==""&&j.length0){return}var o,j,p,l,i,k;o=f(q).position();j=f(q).width();p=f(q).height();l=o.top;i=document.createElement("span");i.className=this.options.errorClass;k=document.createElement("span");k.innerHTML="x";k.className=this.options.errorCloseClass;f(i).empty().css({left:o.left+j+30+"px",top:l+"px"});m.appendChild(i);i.innerHTML=n;i.appendChild(k);this.handler=true},close:function(i){i.parentNode.removeChild(i);this.handler=false}};h.prototype.reset=function(k){var l={};if(typeof k==="undefined"||(k.length>1&&k[0].getAttribute("type")!=="checkbox")){l=f(this.form).find("."+this.options.errorClass)}else{l=f(k[0].parentNode).find("."+this.options.errorClass)}for(var j=l.length-1;j>=0;j--){this.window.close.call(this,l[j])}};h.prototype.clear=function(i){return i.replace(/^\s+|\s+$/g,"")};h.prototype.ajax=function(){var l,j,k=this,i;l=f(this.form).serialize();i=this.form.getAttribute("action");j=(this.options.ajax.url)?this.options.ajax.url:i;if(!this.options.ajax.url&&(i===""||i===null)){return console.log("Form action not valid !")}f.ajax({type:k.options.ajax.type,url:j,data:l,dataType:k.options.ajax.dataType,options:k.options,beforeSend:function(){return k.options.ajax.beforeSend()}}).done(function(m){k.options.ajax.success(k,m)}).fail(function(m,n){k.options.ajax.fail(m,n)}).always(function(m){k.options.ajax.complete(m)})};f.fn.validetta=function(i){if(f.validettaLanguage){e=f.extend(true,{},e,f.validettaLanguage.messages)}return this.each(function(){new h(this,i);return this})}})(jQuery); \ No newline at end of file diff --git a/validetta.jquery.json b/validetta.jquery.json index 95c3cc4..2f5a2c5 100644 --- a/validetta.jquery.json +++ b/validetta.jquery.json @@ -1,6 +1,6 @@ { "name" : "validetta", - "version" : "0.6.0", + "version" : "0.7.0", "title" : "Validetta - A tiny jquery plugin for validate your forms", "description" : "Validetta is a tiny jQuery plugin which you can do client-side validation of your forms. It aims to decrease your burden with easy usage and flexible structure.", "keywords" : ["validetta", "form", "forms", "jquery", "javascript", "validation", "validate"], @@ -8,7 +8,7 @@ "docs" : "http://lab.hasanaydogdu.com/validetta/", "demo" : "http://lab.hasanaydogdu.com/validetta/#examples", "bugs" : "http://github.com/hsnayd/validetta/issues", - "download" : "http://github.com/hsnayd/validetta/archive/v0.6.0.zip", + "download" : "http://github.com/hsnayd/validetta/archive/v0.7.0.zip", "author" : { "name": "Hasan Aydoğdu", "url" : "http://github.com/hsnayd" diff --git a/validetta.js b/validetta.js index 3f443b2..fcaa26d 100644 --- a/validetta.js +++ b/validetta.js @@ -1,6 +1,6 @@ /*! * Validetta - Client-side form validation jQuery plugin - * Version: 0.6.0 (15 November 2013) + * Version: 0.7.0 (12 December 2013) * @jQuery Requires: v1.7 or above * @Browser Support : ie8 or above, and all modern browsers * @@ -149,7 +149,7 @@ * @params {array} _val : current field's value * @params {array} _methods : current field's control methods */ - var _el, _errors, _val = [], _methods = []; + var _el, _errors, _val = [], _methods = []; _el = fields[i]; _errors = ''; _val = $( _el ).val(); @@ -162,8 +162,9 @@ if( _methods[j] === 'required' ){ var _elType = _el.getAttribute('type'); if( _elType === 'checkbox' && !that.check.checkbox.checked( _el ) ){ _errors += messages.checkbox+'
    '; } + else if ( _elType === 'radio' && that.check.radio.call( that, _el ) ) { _errors += messages.empty+'
    '; } else if( _el.tagName ==='SELECT' && !that.check.selectbox.selected( _val ) ){ _errors += messages.selectbox+'
    '; } - if( ( _elType ==='text' || _elType ==='password' || _el.tagName ==='TEXTAREA' ) && !that.check.empty.call( that, _val ) ){ _errors += messages.empty+'
    '; } + if( ( _elType ==='text' || _elType ==='password' || _el.tagName ==='TEXTAREA' ) && !that.check.empty.call( that, _val ) ){ _errors += messages.empty+'
    '; } } // Number Control if( _methods[j] === 'number' && !that.check.number( _val ) ){ @@ -290,7 +291,7 @@ checked : function( _inp ){ return ( !_inp.checked ) ? false : true ; }, - maxChecked : function( _inp, arg ){ + maxChecked : function( _inp, arg ){ var count = $( this.form.querySelectorAll( 'input[type=checkbox][name='+ _inp.name +']' ) ).filter( ':checked' ).length ; return ( count > arg ) ? false : true ; }, @@ -305,12 +306,17 @@ return ( val === '' || val === null ) ? false : true ; }, maxSelected : function( val, arg){ - return ( val !== null && val !== '' && val.length > arg ) ? false : true ; + return ( val !== null && val !== '' && val.length > arg ) ? false : true ; }, minSelected : function( val, arg ){ return ( val !== null && val !== '' && val.length < arg ) ? false : true ; } }, + // Radio + radio : function ( _inp ) { + var count = $( this.form.querySelectorAll( 'input[type=radio][name='+ _inp.name +']' ) ).filter( ':checked' ).length ; + return ( count === 1 ) ? false : true ; + }, // Custom reg check customReg : function( val, reg ){ var _reg = new RegExp( reg ); @@ -357,7 +363,7 @@ errorObject.appendChild( errorCloseObject ); // we have an error so we need to break submit // set to handler true - this.handler = true ; + this.handler = true ; }, /** * @property : close @@ -380,15 +386,15 @@ // if _inp is undefined ( This is the process of resetting all
    ) // or _inp is an object that has element more than one // and these elements are not checkbox - if( typeof _inp === 'undefined' || ( _inp.length > 1 && _inp[0].getAttribute('type') !== 'checkbox' ) ){ - _errorMessages = $(this.form).find( '.'+ this.options.errorClass ); + if( typeof _inp === 'undefined' || ( _inp.length > 1 && _inp[0].getAttribute('type') !== 'checkbox' ) ){ + _errorMessages = $(this.form).find( '.'+ this.options.errorClass ); } - else{ - _errorMessages = $(_inp[0].parentNode).find( '.'+this.options.errorClass ); + else { + _errorMessages = $(_inp[0].parentNode).find( '.'+this.options.errorClass ); } for(var i = _errorMessages.length -1; i >= 0; i--){ this.window.close.call( this, _errorMessages[i] ); - } + } }; /** * @method clear @@ -423,7 +429,7 @@ }) .done( function( result ){ that.options.ajax.success( that, result ); } ) .fail( function( jqXHR, textStatus ){ that.options.ajax.fail( jqXHR, textStatus ); } ) - .always( function( result ){ that.options.ajax.complete( result ); } ); + .always( function( result ){ that.options.ajax.complete( result ); } ); }; /** * Plugin Validetta