1717 * Released under the MIT license
1818 * https://github.com/global-source/javascript_form_validator/blob/master/LICENSE
1919 *
20- * Date: 2017-05-01
20+ * Date: 2017-07-21
2121 */
2222
2323/*
@@ -59,6 +59,8 @@ var jsValidator = {
5959 this . onlyFilter = option . onlyFilter ;
6060 // To Enable/Disable global validator.
6161 this . onChange = option . onChange ;
62+ // Update default response "class".
63+ if ( 'undefined' === typeof option . errorClass ) option . errorClass = 'js-error-cop' ;
6264 // Update "jsSettings" to global object.
6365 this . jsSettings = jsSettings . init ( option ) ;
6466 // Update "jsForm" to global object.
@@ -139,6 +141,7 @@ var jsValidator = {
139141 }
140142 if ( false == this . initialLoad ) validationResponse . init ( errorList , this . option ) ;
141143 this . initialLoad = false ;
144+ helper . scrollToError ( ) ;
142145 return status ;
143146 } ,
144147 /*
@@ -221,7 +224,7 @@ var jsFilter = {
221224 checkStatus : function ( elem ) {
222225 var status ;
223226 status = true ;
224- if ( false === this . forceFilter ) {
227+ if ( false === jsValidator . forceFilter ) {
225228 status = false ;
226229 if ( true === elem . required ) {
227230 status = true ;
@@ -296,8 +299,8 @@ var jsFilter = {
296299 var min = event . target . min ;
297300 var max = event . target . max ;
298301 // Default values for Min and Max.
299- if ( ! min ) min = 0 ;
300- if ( ! max ) max = 54 ;
302+ if ( ! min ) min = 1 ;
303+ if ( ! max ) max = 31 ;
301304 // Forming pattern for Restriction.
302305 var regex = new RegExp ( '^[0-9]+$' ) ;
303306 // Validation with Code.
@@ -309,7 +312,7 @@ var jsFilter = {
309312 event . preventDefault ( ) ;
310313 }
311314
312- var num = + this . value , max = 31 , min = 1 ; //converts value to a Number
315+ var num = + this . value ; //converts value to a Number
313316 if ( ! this . value . length ) return false ; //allows empty field
314317 this . value = isNaN ( num ) ? min : num > max ? max : num < min ? min : num ;
315318
@@ -535,9 +538,9 @@ var jsRuleSets = {
535538 'id' : activeElem . name + '_new1_1_1xv_resp'
536539 } ) ;
537540 firstErrorHit = activeElem . name + '_new1_1_1xv_resp' ;
538- helper . scrollToItem ( '#' + firstErrorHit ) ;
539- } // To Check the Value is less than min or not.
541+ }
540542
543+ // To Check the Value is less than minimum or not.
541544 if ( activeElem . min ) {
542545 if ( jsRuleSets . isSet ( activeElem ) ) {
543546 if ( ! jsRuleSets . min ( activeElem ) ) {
@@ -547,12 +550,12 @@ var jsRuleSets = {
547550 'id' : activeElem . name + '_new1_1_1xv_resp'
548551 } ) ;
549552 firstErrorHit = activeElem . name + '_new1_1_1xv_resp' ;
550- helper . scrollToItem ( '#' + firstErrorHit ) ;
551553 validElem = false ;
552554 }
553555 }
554- } // To Check the Value is grater than max or not.
556+ }
555557
558+ // To Check the Value is grater than max or not.
556559 if ( activeElem . max ) {
557560 if ( jsRuleSets . isSet ( activeElem ) ) {
558561 if ( ! jsRuleSets . max ( activeElem ) ) {
@@ -562,12 +565,12 @@ var jsRuleSets = {
562565 'id' : activeElem . name + '_new1_1_1xv_resp'
563566 } ) ;
564567 firstErrorHit = activeElem . name + '_new1_1_1xv_resp' ;
565- helper . scrollToItem ( '#' + firstErrorHit ) ;
566568 validElem = false ;
567569 }
568570 }
569- } // To Check the Entered E-mail is Valid or Not.
571+ }
570572
573+ // To Check the Entered E-mail is Valid or Not.
571574 if ( activeElem . type == 'email' ) {
572575 if ( jsRuleSets . isSet ( activeElem ) ) {
573576 if ( ! jsRuleSets . email ( activeElem ) ) {
@@ -577,13 +580,13 @@ var jsRuleSets = {
577580 'id' : activeElem . name + '_new1_1_1xv_resp'
578581 } ) ;
579582 firstErrorHit = activeElem . name + '_new1_1_1xv_resp' ;
580- helper . scrollToItem ( '#' + firstErrorHit ) ;
581583 validElem = false ;
582584 }
583585 }
584- } // To Compare the Password is Same or Not with Re-Password.
585- // TODO: Implement Simplified Comparison.
586+ }
586587
588+ // To Compare the Password is Same or Not with Re-Password.
589+ // TODO: Implement Simplified Comparison.
587590 if ( activeElem . type == 'password' ) {
588591 if ( jsRuleSets . isSet ( activeElem ) ) {
589592 if ( ! jsRuleSets . compare ( activeElem ) ) {
@@ -593,7 +596,6 @@ var jsRuleSets = {
593596 'id' : activeElem . name + '_new1_1_1xv_resp'
594597 } ) ;
595598 firstErrorHit = activeElem . name + '_new1_1_1xv_resp' ;
596- helper . scrollToItem ( '#' + firstErrorHit ) ;
597599 validElem = false ;
598600 }
599601 }
@@ -812,12 +814,27 @@ var helper = {
812814 // Finally return "false" for general keys.
813815 return false ;
814816 } ,
817+ /*
818+ * To Scroll Up / Down to notify the item that have validation message.
819+ */
820+ scrollToError : function ( ) {
821+ var active_class = validationResponse . getClass ( ) ;
822+ if ( 0 === document . getElementsByClassName ( active_class ) . length ) return false ;
823+ document . getElementsByClassName ( active_class ) [ 0 ] . setAttribute ( 'id' , '__header_error_target_temp' ) ;
824+ var id = '#' + document . getElementsByClassName ( active_class ) [ 0 ] . id ;
825+ window . location . href = id ;
826+ document . getElementsByClassName ( active_class ) [ 0 ] . removeAttribute ( 'id' ) ;
827+ // Remove the navigated value.
828+ this . removeHash ( id ) ;
829+ } ,
815830 /*
816831 * To Scroll Up / Down to notify the item that have validation message.
817832 */
818833 scrollToItem : function ( item ) {
819834 // Form hash value.
820- var hash = '#' + item ;
835+ var hash = item ;
836+ // If "#" is missing, then add back to the ID.
837+ if ( - 1 === hash . indexOf ( '#' ) ) hash = '#' + hash ;
821838 // Navigate with the hash value.
822839 window . location . href = hash ;
823840 // Remove the navigated value.
@@ -832,7 +849,7 @@ var helper = {
832849 // Replacing the URL with specific hash value.
833850 path = path . replace ( hash , '' ) ;
834851 // Update to url history.
835- window . history . pushState ( '' , 'Title' , path )
852+ window . history . pushState ( '' , 'Title' , path ) ;
836853 }
837854} ;
838855/**
@@ -877,16 +894,20 @@ var pattern = {
877894 * To Manage all kind of error response.
878895 */
879896var validationResponse = {
897+ active_class : false ,
880898 /*
881899 * Initiating the Response handler.
882900 */
883901 init : function ( errorList , option ) {
884902 this . errorMessage = option . message ;
903+ // Updating the class.
904+ this . active_class = option . errorClass ;
885905 // var errorElements = option.errorElem;
886906 // jsLogger.out('Errors', errorList);
887907 this . input ( errorList . input ) ;
888908 this . select ( errorList . select ) ;
889909 this . textArea ( errorList . textArea ) ;
910+
890911 } ,
891912 /*
892913 * To handle the "input" element.
@@ -900,6 +921,9 @@ var validationResponse = {
900921 select : function ( elem ) {
901922 this . process ( elem ) ;
902923 } ,
924+ getClass : function ( ) {
925+ return this . active_class ;
926+ } ,
903927 /*
904928 * To handle the "textArea" element.
905929 */
@@ -911,6 +935,7 @@ var validationResponse = {
911935 */
912936 process : function ( elem ) {
913937 var elementDefaultResponse = '' ;
938+ var active_class = this . getClass ( ) ;
914939 for ( var i in elem ) {
915940 // jsLogger.out('Element', document.getElementById(elem[i].id));
916941 if ( elem [ i ] . el && true === elem [ i ] . el . required ) {
@@ -926,6 +951,7 @@ var validationResponse = {
926951 // jsLogger.out('Element Found', false);
927952 spanTag = document . createElement ( 'span' ) ;
928953 spanTag . setAttribute ( 'id' , activeElem . id ) ;
954+ spanTag . setAttribute ( 'class' , active_class ) ;
929955 spanTag . innerHTML = elementDefaultResponse ;
930956 } else {
931957 // Re-use Existing response Message SPAN.
@@ -974,16 +1000,17 @@ var validationResponse = {
9741000 * then it will be replaces.
9751001 */
9761002 default : function ( errorType ) {
1003+ var active_class = this . getClass ( ) ;
9771004 var errorMessages = {
978- required : 'This field is required' ,
979- min : 'This field length is too low.' ,
980- max : 'This field length is exceeds the limit' ,
981- password : 'Password does not match.' ,
982- email : 'Email is not valid' ,
983- file : 'This file is not allowed'
1005+ required : '<span class="' + active_class + '"> This field is required.</span> ',
1006+ min : '<span class="' + active_class + '"> This field length is too low.</span> ',
1007+ max : '<span class="' + active_class + '"> This field length is exceeds the limit.</span> ',
1008+ password : '<span class="' + active_class + '"> Password does not match.</span> ',
1009+ email : '<span class="' + active_class + '"> Email is not valid.</span> ',
1010+ file : '<span class="' + active_class + '"> This file is not allowed.</span> '
9841011 } ;
9851012 if ( typeof errorType !== 'string' ) return false ;
9861013 if ( typeof errorMessages [ errorType ] === 'undefined' ) return false ;
9871014 return errorMessages [ errorType ] ;
9881015 }
989- } ;
1016+ } ;
0 commit comments