简体   繁体   中英

Sending static data using bootstrapValidator locally

how to use a remote back-end to check if a given username is already taken or not. check and validate locally without using database or web-server

This is latest version of bootstrapValidator, jquery, html5, bootstrap.

<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.2/js/bootstrapValidator.min.js"></script>
<form id="registrationForm" class="form-horizontal">
    <div class="form-group">
        <label class="col-lg-3 control-label">Username</label>
        <div class="col-lg-5">
            <input type="text" class="form-control" name="username" />
        </div>
    </div>
</form>

and custom js is

$(document).ready(function() {
    $('#registrationForm').bootstrapValidator({
        fields: {
            username: {
                message: 'The username is not valid',
                validators: {                    
                    remote: {
                        message: 'The username is not available',
                        url: 'default.php',
                        data: {
                            type: 'username'
                        }
                    }
                }
            }
        }
    });
});

back-end default.php file is

<?php
$isAvailable = true;

switch ($_POST['type']) {
    case 'username':
    default:
        $username = $_POST['username'];
        data = 'admin','root';
        $isAvailable = true;
        break;
}
echo json_encode(array(
    'valid' => $isAvailable,
));

username already exists or not from default.php file, error not showing.

$(document).ready(function() {
    $('#registrationForm').bootstrapValidator({
        fields: {
            username: {
                message: 'The username is not valid',
                validators: {                    
                  callback: {
                    callback: function(value, validator) {
                      var data = "user1";
                      if (value === data) {
                        return {
                          valid: false,
                          message: 'The name is not available'
                        }
                      }                 
                      return true;
                    }
                  }
                }
            }
        }
    });
});

Instead of using php function, I have used call back function for this it's working fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM