Please include text beneath the input group addon class

Hey everyone, I'm facing some issues with my layout. Whenever the input fields are empty or null, I encounter errors. I want to ensure that the input text and input group addon have equal heights. Also, upon clicking the submit button, I use jquery/ajax to display errors if the fields are empty. Is there a way to place the error message under the input group class? Any suggestions on how to achieve this would be greatly appreciated. Thank you!

Sample Image

public function insert_officials() {
    $data = array('success' => false, 'messages' => array());

    $name   = array('official_name','official_contact','official_position');
    $label  = array('Name', 'Phone', 'Position');
    $verify = 'trim|required';
    $number = 'required|regex_match[/^[0-9]{10}$/]';
    $this->validate($name[0], $label[0], $verify);
    $this->validate($name[1], $label[1], $number);
    $this->validate($name[2], $label[2], $verify);
    $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');

    if($this->form_validation->run()) {
        $data['success'] = true;
    } else {
        foreach($_POST as $key => $value) {
            $data['messages'][$key] = form_error($key);
        }
        echo json_encode($data);
        exit;
    }

    $data = array (
        $name[0] = $this->pass($name[0]),
        $name[1] = $this->pass($name[1]),
        $name[2] = $this->pass($name[2])
    );

    //$this->model->CreateOfficials($data);
}

 

    function add_officials() {
    $('#FormAddOfficials').submit(function(e) {
        e.preventDefault();
        var url = $(this);
        var data = {
            official_name : $('#official_name').val(),
            official_position : $('#official_position').val(),
            official_contact : $('#official_contact').val()
        }
        $.ajax({
            type: 'POST', url: url.attr('action'), data: data, cache: false, dataType: 'json',
            success:function(data) {
                if(data.success == true) {
                    successful(data.success,data.bgcolor,data.color,data.message);
                } else {
                    $.each(data.messages, function(key,value) {
                        //key => id of an input, value => error message
                        var element = $('#' + key);

                        element.closest('div.form-group')
                        .removeClass('has-error')
                        .addClass(value.length > 0 ? 'has-error' : 'has-success')
                        .find('.text-danger').remove();
                        element.after(value);
                    });
                }
            }
        });
    }); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="<?=base_url();?>execute/insert" id="FormAddOfficials" name="FormAddOfficials" autocomplete="off" class="nobottommargin" method="POST">
    <div class="row">
        <div class="col-lg-4 col-md-8 col-xs-12">
           <div class="form-group">
              <label class="control-label" for="official_name">Name<small> *</small></label>
              <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-user-o" aria-hidden="true"></i></span>
                <input type="text" class="form-control" name="official_name" id="official_name" placeholder="Enter name">    
              </div>
           </div>
        </div>
          <div class="col-lg-4 col-md-8 col-xs-12">
             <div class="form-group">
                <label class="control-label" for="official_contact">Phone<small> *</small></label>
                <div class="input-group">
                   <span class="input-group-addon"><i class="fa fa-phone" aria-hidden="true"></i></span>
                   <input type="text" class="form-control" name="official_contact" id="official_contact" placeholder="Enter phone">
                   **<p>Error message will go here!!</p>**  
                </div>
                ***<p>I want to place the paragraph here!</p>***
             </div>
          </div>
          <div class="col-lg-4 col-md-8 col-xs-12">
             <div class="form-group">
                <label class="control-label" for="official_position">Position<small> *</small></label>
                <div class="input-group">
                   <span class="input-group-addon"><i class="fa fa-address-card-o" aria-hidden="true"></i></span>
                   <select class="form-control" name="official_position" id="official_position">
                      <option value="" hidden>Select position</option>
                      <option>asd</option>
                      <option>qwe</option>
                   </select>
                </div>
             </div>
          </div>
       </div>
       <div class="margintop-xs">
          <button type="submit" class="btn btn-success" id="btn-officials"><i class="fa fa-check" aria-hidden="true"></i>Submit</button>
          <button type="reset" class="btn btn-default" id="btn-reset">Cancel</button>
       </div>
   </form>

Answer №1

Have you tried adding the error message to the .input-group class? This could potentially solve the issue.

var element = $('#' + key).closest('.input-group');

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

Error message: The Bootstrap .dropdown() method failed because it encountered an "Uncaught TypeError: undefined is not a function"

I've encountered an issue that seems to be a bit different from what others have experienced. Despite trying various solutions, I still can't seem to fix it. I suspect it might have something to do with how I'm importing my plugins. The erro ...

What are the steps to integrate MaterializeCss into Vue.js?

I prefer not to utilize Vue-Material or Vuetify. My choice is Materialize. Here's what I do: npm install materialize-css@next In my main.js file, where I define my new Vue App, I import Materialize like this: import 'materialize-css' Th ...

Looking to adjust the HSL of individual colors on a JavaScript canvas, similar to editing in Photoshop?

I'm looking to manipulate the HSL of an image using Javascript. I don't want to apply changes to the entire image at once, but rather target specific color sets like blues and yellows, similar to how it's done in Photoshop. What type of da ...

Experience seamless navigation with Highstock's fluid panning feature

I'm attempting to achieve seamless panning on a basic highstock chart by clicking and dragging within the plot area. Interestingly, I have found that this works flawlessly when my data is not based on timestamps: data: [-25.1,-23.8,-19.9,-19.1,-19.1 ...

"Error: The property $notify is not found in the type" - Unable to utilize an npm package in Vue application

Currently integrating this npm package for notification functionalities in my Vue application. Despite following the setup instructions and adding necessary implementations in the main.ts, encountering an error message when attempting to utilize its featur ...

The functionality ceases to operate properly once a new element has been added

I am encountering an issue with the "click" action on a newly created element through a jQuery function, as it is not functioning properly. To demonstrate this problem, I have set up a JSFiddle at the following link (JSFiddle), however, please note that t ...

Making an asynchronous call from Index.html to PHP Script

I am currently working on implementing an AJAX call to my PHP Script. While I can successfully echo the results from my data.php file, I am now facing a challenge regarding how to initiate the call from index.html in order to retrieve the table results s ...

Tips for concealing items on a website within a WebView

Is it possible to hide the header element with the search bar and logo when loading this page on a Webview? I'm not familiar with JavaScript, so is there a way to achieve this? The section I am looking to conceal ...

Align an image in the middle with a heading

I am currently working on aligning a picture and heading in a header section. My goal is to center both elements, with the picture being twice as tall as the heading. So far, I have them stacked one above the other: .body { font: 15px/1.5 Arial, Helveti ...

Guide on incorporating markdown in an ejs template

As I am planning to create a small blog using Express, EJS, and Markdown, I encountered an issue when trying to load Markdown on the page. Here is what I saw: https://i.sstatic.net/M2FEK.png Here's my code snippet: <!DOCTYPE html> <html la ...

What is the function of async in Next.js when triggered by an onClick

Need help with calling an async function pushData() from a button onClick event async function pushData() { alert("wee"); console.log("pushing data"); try { await query(` //SQL CODE `); console.log("Done&quo ...

I am trying to access the id of the button that was selected, but I am unsure of how to retrieve the id from it. The method of using

I am trying to retrieve the id of a selected button for deletion purposes. However, I am unable to get the id from it using 'this.id'. Is there another method I should be using? Below is the code where I create the button: var deleteEmployer= d ...

What could be improved in this AngularJS code snippet?

Currently, I am immersed in an AngularJS book and have extracted the following code snippet directly from its pages: <!DOCTYPE html> <html ng-app='myApp'> <head> <title>Your Shopping Cart</title> </head> & ...

How can you retrieve attributes from a specific element within a dropdown menu?

I came across this intriguing HTML code snippet: <select id="selectSomething"> <option id="4" data-name="tomato" data-email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfbba0a2aebba08fbbe1aca0a2">[email ...

PHP code exhibiting inconsistent behavior when executed on a WAMP Server

My WAMP Server is set up and running smoothly as I dive into PHP code for a class assignment. The server icon shines bright green, my HTML and PHP files in the www folder are functioning perfectly. I am currently using PHP v7.0.1 on my WAMP setup. However ...

Execute a script once the AngularJS view has finished loading

Just starting out with AngularJS and I've encountered an issue; I'm populating part of my view with HTML from a variable in my controller: <div ng-bind-html="deliberatelyTrustDangerousSnippet()"></div> Everything works as expected ...

I am currently working on creating a system for displaying rating stars for products. The rating value, which ranges from 0 to 5, has already been provided

class PostBodyRatingStars extends React.Component { render() { const { classes } = this.props var rating = this.props.rating function createMappingArray(rating) { var arr = [] for (i = 0; i < 5; i++) { if (rating >= ...

Tips for updating the current user's username value in local storage in Angular

https://i.stack.imgur.com/ZA32X.png https://i.stack.imgur.com/iDHZW.png I am facing an issue with updating the local storage of a current User for only username. When I update the username, it's not reflecting in local storage. Even though I can s ...

Error: The studentsList is not iterable. Issue encountered during Jasmine Unit Testing in Angular 2

How can I effectively test the forEach loop in jasmine with karma? Below is the code for the component: getData(studentsList:any, day:any, game:any){ let strength:any; let location:string; if(studentsList){ studentsList.forEach( value =& ...

Is it possible to simultaneously update two entities using a single endpoint?

In order to update data in two different entities with a @OneToOne relationship between UserEntity and DetailsEntity, I need to create a function in my service that interacts with the database. Here are the entity definitions: UserEntity @Entity() export ...