Browse Source

#191: Add another selector example

nghuuphuoc 11 years ago
parent
commit
395ffae0a4
1 changed files with 90 additions and 0 deletions
  1. 90 0
      demo/selector2.html

+ 90 - 0
demo/selector2.html

@@ -0,0 +1,90 @@
+
+<!DOCTYPE html>
+<html>
+<head>
+    <title>BootstrapValidator demo</title>
+
+    <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
+    <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
+
+    <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
+    <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
+    <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
+</head>
+<body>
+    <div class="container">
+        <div class="row">
+            <section>
+                <div class="col-lg-8 col-lg-offset-2">
+                    <div class="page-header">
+                        <h2><code>selector</code> example</h2>
+                    </div>
+
+                    <form id="sumForm" method="post" class="form-horizontal" action="target.php">
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Percentage</label>
+                            <div class="col-lg-5">
+                                <input class="form-control percent" type="text" />
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <div class="col-lg-offset-3 col-lg-5">
+                                <input class="form-control percent" type="text" />
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <div class="col-lg-offset-3 col-lg-5">
+                                <input class="form-control percent" type="text" />
+                            </div>
+                        </div>
+                        <div class="form-group">
+                            <div class="col-lg-9 col-lg-offset-3">
+                                <button type="submit" class="btn btn-primary">Validate</button>
+                            </div>
+                        </div>
+                    </form>
+                </div>
+            </section>
+        </div>
+    </div>
+
+<script type="text/javascript">
+$(document).ready(function() {
+    $('#sumForm').bootstrapValidator({
+        feedbackIcons: {
+            valid: 'glyphicon glyphicon-ok',
+            invalid: 'glyphicon glyphicon-remove',
+            validating: 'glyphicon glyphicon-refresh'
+        },
+        fields: {
+            'percentage': {
+                selector: '.percent',
+                validators: {
+                    notEmpty: {
+                        message: 'The percentage is required'
+                    },
+                    callback: {
+                        message: 'The sum must be 100',
+                        callback: function(value, validator) {
+                            var percentage = validator.getFieldElements('percentage'),
+                                length     = percentage.length,
+                                sum        = 0;
+                            for (var i = 0; i < length; i++) {
+                                sum += parseFloat($(percentage[i]).val());
+                            }
+                            if (sum == 100) {
+                                validator.updateStatus('percentage', 'VALID', 'callback');
+                                return true;
+                            }
+
+                            return false;
+                        }
+                    }
+                }
+            }
+        }
+    });
+});
+</script>
+</body>
+</html>