Fixing the bootstrapValidator error in a PHP file: A step-by-step guide

Having trouble with bootstrapValidation()

I created a PHP file and included all the Bootstrap 4.0 CDNs, then made a separate JavaScript file where I created a function called bootstrapValidator to check input length and emptiness using jQuery and Ajax. However, I'm getting an error message that says Uncaught ReferenceError: bootstrapValidator is not defined at :1:1

<!DOCTYPE html>
<html>
<head>
   <!-- Included the CSS CDN for Bootstrap 4.0.0 -->
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

   <!-- Online Font Awesome icons -->
   <link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet" >

</head>
<body>
<div style="width: 50%; margin: 20px auto">
  <form class="form needs-validation" action="" method="POST" id="contact-form" novalidate>

    <div class="input-group mb-3 size">
      <div class="input-group-prepend">
        <span class="input-group-text"><i class="fas fa-user"></i></span>
      </div>
      <input type="text" class="form-control" placeholder="First Name" name="first_name" required>
      <div class="invalid-feedback">Please enter your First Name</div>
    </div>

    <div class="input-group mb-3 size">
      <div class="input-group-prepend">
        <span class="input-group-text"><i class="fas fa-user"></i></span>
      </div>
      <input type="text" class="form-control" placeholder="Last Name" name="last_name" required>
      <div class="invalid-feedback">Please enter your Last Name</div>
    </div>

    <button type="submit" class="btn btn-lg btn-primary text-align-center btn-block"><i class="fas fa-paper-plane"></i> Send</button>

  </form>
</div>



<!-- The external script in a separate .js file -->
<script>
   $(document).ready(function() {
  //alert("HEllo Nehal");
  $("#contact-form").bootstrapValidator({
    feedbackIcons: {
      // valid: 'glyphicon glyphicon-ok',
      // invalid: 'glyphicon glyphicon-remove',
      // validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
      first_name: {
        validators: {
          stringLength: {
            min: 10,
            message: "First Name should be at least 10 characters long"
          },
          notEmpty: {
            message: "Please Enter Your First Name"
          }
        }
      },
      last_name: {
        validators: {
          stringLength: {
            min: 10,
            message: "Last Name should be at least 10 character long"
          },
          notEmpty: {
            message: "please Enter your Last Name"
          }
        }
      }
    }
  })
});

</script>

// External script ends here    

<!-- For Bootstrap validation -->
    <script src="https://cdn.rawgit.com/PascaleBeier/bootstrap-validate/v2.2.0/dist/bootstrap-validate.js" ></script>

    <!-- Online JavaScript CDN for Bootstrap 4.0.0 -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>
</html>

Answer №1

Please consider the following solution:

<head>
    <!-- Load CSS CDN for bootstrap 4.0.0 -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <!-- Include online icons from fontawesome -->
    <link href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" rel="stylesheet" >
    <!-- jQuery and Bootstrap JS -->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <!-- BootstrapValidator JS -->
    <script type="text/javascript" src="https://cdn.jsdelivr.net/jquery.bootstrapvalidator/0.5.0/js/bootstrapValidator.min.js"></script>
    <!-- Online javaScript CDN for bootstrap 4.0.0 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
</head>

    <body>
    <div style="width: 50%; margin: 20px auto">

<form method="post" class="form-horizontal" id="contact-form"
data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
data-bv-feedbackicons-validating="glyphicon glyphicon-refresh">
<div class="input-group mb-3 size">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input type="text" class="form-control" placeholder="First Name" name="first_name" required
data-bv-notempty="true"
data-bv-notempty-message="The username is required and cannot be empty">
<div class="invalid-feedback">Please enter your First Name</div>
</div>

<div class="input-group mb-3 size">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-user"></i></span>
</div>
<input type="text" class="form-control" placeholder="Last Name" name="last_name" required
data-bv-notempty="true"
data-bv-notempty-message="The username is required and cannot be empty">
<div class="invalid-feedback">Please enter your Last Name</div>
</div>

<button type="submit" class="btn btn-lg btn-primary text-align-center btn-block"><i class="fas fa-paper-plane"></i> Send</button>

</form>
</div>
    </body>

    <!-- This script is in an external .js file -->
<script>
   $(document).ready(function() {
  //alert("Hello Nehal");
  $('#registrationForm').bootstrapValidator();
});

</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving content through jQuery Ajax with caching

Let's simplify this situation. 1) I have 3 links that trigger an Ajax Request to update a specific div with content. The DIV <div id="content-to-update"></div> The 3 links updating #content-to-update <a href="#" onClick="execute ...

Android Font Icon Spacing Issue with IconFont (Fontastic)

After using Fontastic.me to create an iconfont, I encountered an issue specifically with the native browser of Android 4.2.2 and 4.3 (such as modern Samsung tablets). In these browsers, the characters in the entire font appear to have no width. This proble ...

The height of the input element is greater than the size of the font

I am facing an issue with an input element that has a height 2px higher than what I expected. Here is the relevant CSS: .input { font-size: 16px; padding: 15px; border: 1px solid black; border-radius: 3px; width: 400px; } <input class=" ...

What advantages does Snap.svg offer compared to the HTML5 native SVG DOM API?

Can the differences between Snap.svg and HTML5 native SVG DOM API be compared to jQuery vs HTML5 native DOM? Apart from potential cross-browser disparities, assuming modern browsers support SVG well, what advantages does Snap.svg offer? What are the pros ...

Trouble arises when trying to showcase a div tag with the jquery colorbox

Check out this HTML snippet I created: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="css/ ...

Having trouble with the Ng multiselect dropdown displaying empty options?

I'm currently facing a challenge in adding a multiselect dropdown feature to my project. Below is the code I have been working on: HTML <ng-multiselect-dropdown [settings]="searchSettings" [data]="dummyList" multiple> </n ...

Discover the procedure to alter date formats in MongoDB

I recently created a MongoDB collection using Node.js with the following code: var mongoose = require('mongoose'); var TTTUsersSchema = new mongoose.Schema({ username: String, password:String, active: Boolean, created_at: { type: Dat ...

Run JavaScript function when an ajax request encounters an error

When using the ajax function to call a webservice and encountering an error, my goal is to trigger the userCreate() javascript function. $.ajax({ type:"POST", beforeSend: function (request) { request.setRequestHeader("X-DreamFactory-Applic ...

Instead of only one menu icon, now there are three menu icons displayed on the screen. (Additionally, two more divs containing

When visiting on a smartphone browser, you may notice that instead of one menu icon, three icons appear. Upon inspecting the elements, you will find that there are three div's that look like this: <div class="responsive-menu-icon">&l ...

Customizing table header sort icons in PrimeNG 15.4+: A step-by-step guide

The most recent update in PrimeNG brought about a change in the way sort icons are implemented. Previously, they used an i tag with CSS classes which could be customized easily using my company's icons: https://i.sstatic.net/f0Nrq.png However, the n ...

Can integer values be stored in localStorage similar to JavaScript objects and retrieved without requiring typecasting?

After setting an integer value to a localStorage item: localStorage.setItem('a', 1) and checking its data type: typeof(localStorage.a) "string" it shows as a string. I then typecast it to an int for my purposes: parseInt(localStorage.a) My ...

What is the best way to show search results in real-time as a user

While working on a search box feature that filters and displays results as the user types, I encountered an issue where the search results keep toggling between showing and hiding with each letter input. As a beginner in JS, I am hoping for a simple soluti ...

The AngularJS price slider may exceed its range if the ng-model is null or below the minimum value

I currently have an rz-slider featured on my webpage that is utilized for gathering the price of a product from the user. In addition to the slider, there are two input fields present which are designated for storing the minimum and maximum values. The ng- ...

Using Jquery to dynamically add or remove a class based on scrolling behavior

Can someone help me with identifying the position of an element so that when the user scrolls to it and it appears on the screen, an icon can be highlighted? Currently, the script I am using adds the class too early, closer to the bottom of the block. I w ...

"Encountering a 400 Bad Request error when attempting an Ajax form submission in a Ruby

I'm encountering a 400 Bad Request error while trying to submit a form using Ajax. Here is the form snippet: <%= form_tag client_contacts_path, role: 'form', id: 'nested_client_contact_form', remote: true, method: 'post&a ...

What is the best way to trigger a function in my JavaScript code when the console log is opened on my webpage?

I have a requirement to hide the username and password fields in a form from being viewed in the console log. My plan is to listen for the console log event and then remove the values from these fields using jQuery. Any tips on how to achieve this? Thank ...

jquery token selection dropdown box

I am not encountering any errors with this particular issue, however upon reviewing the plugin it appears that it should only toggle "admin admin" in the dropdown list. I am currently utilizing the most recent version of jquery. Upon further investigation ...

Error: The absence of type definition

I am struggling to implement the functionality of an Add Question button, encountering an error message that reads: TypeError: form.questionText is undefined Can anyone point out where I went wrong? <script type="text/javascript"> fun ...

What are some of the top languages, libraries, and modules used to create a web application for constructing templates?

Our company specializes in creating websites for membership-based associations. Currently, we utilize custom DLL's created with CodeGear Delphi to connect with a SQL Server database and implement a unique HTML template language. Essentially, our templ ...

Displaying a newly inserted span element

When seeking advice on adding a click event to dynamically added elements, I discovered the use of on() for event delegation. Despite this, I am encountering difficulty targeting the dynamically created content, particularly a span with a class desc. This ...