0

how to use a remote back-end to check if a given username is already taken or not.

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,
));

Not showing username already exists or not from default.php file

  • The error is because you're working on the local file system (ie. the `file://` protocol). This raises security issues in most browsers. To fix the problem make the request to a web server. You can easily install one. If you're on Windows Pro, IIS comes as standard, you just need to enable it. Otherwise research XAMPP – Rory McCrossan Aug 27 '19 at 08:00
  • @ Rory McCrossan Thanks and For the entered value in username is available or not in username filed. Dummy value is not showing error or any error message. – sampath_sam10 Aug 27 '19 at 08:59

0 Answers0