fixing the donation form javascript validation
authorAnthony Talarico <talarico@gaslightmedia.com>
Wed, 3 Jan 2018 12:46:53 +0000 (07:46 -0500)
committerAnthony Talarico <talarico@gaslightmedia.com>
Wed, 3 Jan 2018 12:46:53 +0000 (07:46 -0500)
adding more javascript validation to the donation page code for the amount input

js/app.js
js/custom/donate.js
package.json
parts/header-meta.php
style.css

index 2ddcb3e..cfa7539 100644 (file)
--- a/js/app.js
+++ b/js/app.js
@@ -22,7 +22,10 @@ var g=a(f(this.selector(c),a("<div></div>").html(c.attr("title")).html())),h=thi
         var add_remove = page.find('.gfield_list_icons').children('img');
         var total_input = page.find(".ginput_container_total");
         var total = 0;
-        
+        var form = $('#gform_3');
+        var missing_value = false;
+        var amount = 0;
+
         // set up autocomplete
         $('.gfield_list_2_cell2').children().autocomplete({source: tags});
         
@@ -45,28 +48,66 @@ var g=a(f(this.selector(c),a("<div></div>").html(c.attr("title")).html())),h=thi
 
         $(donate_fields).on('blur', '.gfield_list_2_cell1 > input', function(){
             var _ = $(this);
+            var fund_field = _.parent().next().children('input');
             var amount_input = page.find(".gfield_list_2_cell1").children();
             var rows = amount_input.length;
 
+            var stripped = strip_nan( _.val() );
+            stripped = parseFloat(stripped);
+
+            //set amount to 0 if NaN
+            if( isNaN( stripped ) ){
+                $(_).val('');
+            }else{
+                $(_).val(stripped);
+            }
+
             total = 0;
             $.each(amount_input, function(index, value){
-                
+                var fund = $(this).parent().next().children('input').val();
                 var amount = $(this).val();
+
                 amount = strip_nan(amount);
                 amount = parseFloat(amount);
-                if(amount && !isNaN(amount)){
+
+                if( isNaN(amount)){
+                    amount = 0;
+                }
+                
+                if(amount >= 0 && !isNaN(amount)){
                     total += amount;
                     total_input.children('#input_3_1').val(total.toFixed(2));
                     total_input.children( '.ginput_total' ).text('$' + total.toFixed(2));
-                } else if(rows <= 1) {
-                    total_input.children('#input_3_1').val(0);
-                    total_input.children( '.ginput_total' ).text('$0.00');
-                    total = 0;
-                }
+                } 
+                
             });
+
             $('.gfield_hidden_product').last()
                 .find( $("[id^='ginput_base']") ).val('$' + total.toFixed(2));
         });
+
+        $("#gform_submit_button_3").on("click", function(e){
+            if(total <= 0){
+                alert("Please enter an amount greater than 0");
+                return false;
+            } else{
+                $(form).on("submit").submit();
+            }
+        })
+
+
+
+        // $(document).on('submit',form, function(e){
+        //     console.log("asdf");
+        //     if(missing_value){
+        //         alert("Please enter an amount greater than 0");
+        //         e.preventDefault();
+        //         // $(this).unbind('submit');
+        //     } else { 
+        //         return
+        //     }
+        // })
+
     });
 });;// Load foundation
 $(document).foundation();
index e1fddfb..a57794e 100644 (file)
@@ -13,7 +13,10 @@ $(function(){
         var add_remove = page.find('.gfield_list_icons').children('img');
         var total_input = page.find(".ginput_container_total");
         var total = 0;
-        
+        var form = $('#gform_3');
+        var missing_value = false;
+        var amount = 0;
+
         // set up autocomplete
         $('.gfield_list_2_cell2').children().autocomplete({source: tags});
         
@@ -36,27 +39,65 @@ $(function(){
 
         $(donate_fields).on('blur', '.gfield_list_2_cell1 > input', function(){
             var _ = $(this);
+            var fund_field = _.parent().next().children('input');
             var amount_input = page.find(".gfield_list_2_cell1").children();
             var rows = amount_input.length;
 
+            var stripped = strip_nan( _.val() );
+            stripped = parseFloat(stripped);
+
+            //set amount to 0 if NaN
+            if( isNaN( stripped ) ){
+                $(_).val('');
+            }else{
+                $(_).val(stripped);
+            }
+
             total = 0;
             $.each(amount_input, function(index, value){
-                
+                var fund = $(this).parent().next().children('input').val();
                 var amount = $(this).val();
+
                 amount = strip_nan(amount);
                 amount = parseFloat(amount);
-                if(amount && !isNaN(amount)){
+
+                if( isNaN(amount)){
+                    amount = 0;
+                }
+                
+                if(amount >= 0 && !isNaN(amount)){
                     total += amount;
                     total_input.children('#input_3_1').val(total.toFixed(2));
                     total_input.children( '.ginput_total' ).text('$' + total.toFixed(2));
-                } else if(rows <= 1) {
-                    total_input.children('#input_3_1').val(0);
-                    total_input.children( '.ginput_total' ).text('$0.00');
-                    total = 0;
-                }
+                } 
+                
             });
+
             $('.gfield_hidden_product').last()
                 .find( $("[id^='ginput_base']") ).val('$' + total.toFixed(2));
         });
+
+        $("#gform_submit_button_3").on("click", function(e){
+            if(total <= 0){
+                alert("Please enter an amount greater than 0");
+                return false;
+            } else{
+                $(form).on("submit").submit();
+            }
+        })
+
+
+
+        // $(document).on('submit',form, function(e){
+        //     console.log("asdf");
+        //     if(missing_value){
+        //         alert("Please enter an amount greater than 0");
+        //         e.preventDefault();
+        //         // $(this).unbind('submit');
+        //     } else { 
+        //         return
+        //     }
+        // })
+
     });
 });
\ No newline at end of file
index 0a67921..2d7df3d 100644 (file)
@@ -10,5 +10,9 @@
     "grunt-contrib-uglify": "~0.7.0",
     "grunt-contrib-concat": "~0.5.0",
     "grunt-contrib-copy": "~0.7.0"
+  },
+  "dependencies": {
+    "request": "~2.81.0",
+    "node-sass": "~4.7.2"
   }
-}
\ No newline at end of file
+}
index 00f2d5a..3871eda 100644 (file)
@@ -2,7 +2,7 @@
     <meta charset="utf-8" />
     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
     <title><?php wp_title(); ?></title>
-    <link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri() ; ?>/css/app.css?v=1.0.6">
+    <link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri() ; ?>/css/app.css?v=1.0.7">
     <link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri() ; ?>/favicon.ico">
     <link href='//fonts.googleapis.com/css?family=Open+Sans:400,400italic,700' rel='stylesheet' type='text/css'>
     <?php wp_head(); ?>
index 3c9366c..e606b0c 100644 (file)
--- a/style.css
+++ b/style.css
@@ -3,5 +3,5 @@ Theme Name: Petoskey-HarborSpringsCommunityFoundation
 Author: Gaslight Media
 Author URI: http://www.gaslightmedia.com
 Description: A theme for Petoskey-HarborSpringsCommunityFoundation
-Version: 1.0.6
+Version: 1.0.7
 */