Ensure that the textbox remains valid when the reset button is clicked in Bootstrap

After implementing the code for bootstrap textbox and textarea, I encountered an issue when using the reset button. When entering an invalid shop name followed by a valid address and clicking on the Reset button, it resets the form. However, if I then only enter an invalid shop name and click on "Add Shop," it successfully adds a new shop. The textboxes continue to display a green background-color and correct sign even when empty. Any assistance would be greatly appreciated.

https://i.stack.imgur.com/VlyQG.jpg


<form id="defaultForm" method="post" class="form-horizontal" 
      data-bv-message="This value is not valid"
      data-bv-feedbackicons-valid="glyphicon glyphicon-ok"
      data-bv-feedbackicons-invalid="glyphicon glyphicon-remove"
      data-bv-feedbackicons-validating="glyphicon glyphicon-refresh">
    <div class="form-group">
        <label class="col-xs-3 control-label">Shop Name</label>
        <div class="col-xs-4">
            <input type="text" class="form-control" pattern="^[a-zA-Z\s]+$"
            data-bv-regexp-message="The shop name can consist of alphabetical characters only" name="shopName" placeholder="Shop Name" data-bv-trigger="blur" data-bv-notempty="true" data-bv-notempty-message="Shop name is required and cannot be empty" />
        </div>
    </div>
    <div class="form-group">
        <label class="col-xs-3 control-label">Shop Address</label>
        <div class="col-xs-4">
            <textarea id="id_txt_addr" type="text" class="form-control" name="shop_address" placeholder="Shop Address" style="height:100px" maxlength="200" data-bv-trigger="blur" data-bv-notempty="true" data-bv-notempty-message="Shop address is required and cannot be empty"></textarea>
        </div>
    </div>

    <div class="form-group">
        <div class="col-xs-9 col-xs-offset-3">
            <input type="submit" name="add" class="btn btn-primary"  value="Add Shop">
            <button type="reset" class="btn btn-default">Reset</button>
        </div>
    </div>
</form>

Answer №1

When the reset button is clicked, all validation classes and error message handling elements closest to the form should be removed. See below for an example:

$(':reset').on('click', function(){
    var $form = $(this).closest("form");
    $form.find('*').removeClass('has-success has-error glyphicon-ok glyphicon-remove');
    $form.find('.help-block').remove();
});

-DEMO-

There may be a bootstrap method available for resetting validation, it's advisable to consult the bootstrap documentation.

Answer №2

  $(document).ready(function () {
        $("#btnReset").on("click", function () {
            $('#CompanyName').val("");
            $('#address').val("");
            $('.icon ').remove();
        });
    });

Clicking the reset button will clear both fields. Hopefully this information proves useful.

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

Expanding the capabilities of search and replace in Javascript is imperative for enhancing its

I have developed a search and replace function. How can I enhance it by adding a comment or alert to describe the pattern and incorporating a functional input box? Any suggestions are welcome! <html> <head> <title> Search & Replace ...

Make sure that the function displays the output once it has been received using the jQuery.get method

This script fetches data using the $.get method from an XML file on an ASP ashx server: $.get("http://www.example.com/example.ashx", { From: txtFrom, To: txtTo, Date: txtTime }, function (data) { var arr ...

I am looking to create a cascading dropdown list in C# MVC that pulls data from various SQL tables. How can I achieve

I am currently working on a C# MVC web application and facing a challenge in implementing a cascading drop-down list. While I have come across various articles discussing this topic, the uniqueness of my requirements makes it difficult for me to figure out ...

struggling to implement captcha verification with jQuery validation

I've been working on real-time captcha validation using jQuery Validation version 1.11.1. The captcha value is stored in a session variable called security_code. I'm utilizing the remote method of jQuery for this purpose, which is my first time u ...

The matMul function encountered an error due to a mismatch in the inner shapes of the Tensors. The shapes provided were 684,1 and 2,1, and the transposeA and transposeB parameters were both set

I am completely new to the world of AI and tensorflow.js. Currently, I am following a Machine Learning course by Stephen Grider. I was expecting an output after running the code below, but instead, I encountered an error. Can someone please assist me? Her ...

Has anyone come across an XSLT that can effectively convert Apple Pages files into high-quality HTML?

When Apple’s word processor/DTP app Pages saves its files, it does so as zipped folders with an XML file containing the content and attachments like images stored as separate files. I am interested in converting this content into HTML format, using < ...

Determine whether a click event originated from within a child window

Currently, I am utilizing window.open to initiate a new window in javascript and my goal is to identify clicks made within the child window. Essentially, if a click event occurs in the child window, I aim to modify the parent window accordingly. I have a ...

Instructions on utilizing the CSSStyleSheet.insertRule() method for modifying a :root attribute

Is it possible to dynamically set the background color of the :root CSS property in an HTML file based on a hash present in the URL? The code provided does change the background color, but unfortunately, the hash value doesn't persist as users navigat ...

"Including Span icons, glyphs, or graphs within a table cell in AngularJS based on a specified condition or numerical

I'm attempting to incorporate span icons/glyph-icons using Angular within a td before displaying the country. I've utilized the code below to achieve this, but I'm looking for a more dynamic and elegant solution. Your assistance is greatly a ...

Another method to verify an input using HTML5 regex and JavaScript

When working with vanilla JavaScript to check an HTML input against a regex pattern, things can get tricky. I find myself going back to the parent element to validate the input, and while it works, it's not as elegant as I would like it to be. Here i ...

What are the best ways to enhance performance for ajax requests using Jquery?

Currently, I am in the process of developing a mobile application that utilizes jquery mobile, jquery, and a PHP backend. My issue arises when dealing with certain pages as there are numerous ajax requests being sent and received simultaneously, resulting ...

How to Generate a Doughnut Graph with JSON and the chart.js Library

Exploring chart.js for analytics is a new experience for me. I've been searching online for a comprehensive guide on how to pass JSON data from an SQL Database using LINQ Lambda expression into chart.js within a .NET environment. Unfortunately, I have ...

Issues arise when attempting to display Sphinx documentation containing SVG images on Internet Explorer 11

I have been working on building Sphinx documentation and incorporating a UML diagram generated as an SVG image using graphviz. The SVG file is embedded in the auto-generated HTML template from Sphinx. While the SVG displays perfectly fine on Chrome and Fi ...

The scrolling speed of the mousewheel in Firefox is notably slower compared to that of Google Chrome

Kindly review this sample link: When I test the above example in Chrome and scroll using the mouse wheel, the page moves up by 100px each time. The Y position is displayed as well. However, if I try the same page in Firefox 26.0 and scroll with the mouse ...

Accessing CSV data stored externally using JavaScript

Hi there, I am struggling to load external CSV data into a script due to what I believe is the browser's same origin policy restriction. I came across some information about using cross-document messaging as a workaround, but I have no idea how to go ...

Exploring the Benefits of Using Gatsby with Material-UI: The Importance of Creating a Page

Upon reviewing the gatsby demo showcased on the material-ui project github page, I found myself puzzled by a few lines of code. In the specific file getPageContext.js export default function getPageContext() { // Ensuring each server-side request has i ...

One click wonder: Easily print any webpage content with just the click of a button!

For example, upon clicking the button on my webpage, I want the content of another page to be sent directly to a printer. The objective is to bypass the print dialog box and print preview that typically appears when using the browser's default printin ...

Unable to choose anything within a draggable modal window

Can someone please assist me? I am struggling to figure this out. I can't select text inside the modal or click on the input to type text into it. I suspect it may be due to z-index issues, but I'm having trouble locating them. var currentZ = n ...

Display the keys of a nested array in Vue.js when the structure is unknown

Here is a representation of a dynamic array I have: nodes: [ { n1: "Foods", }, { n4: "Drinks", b7: [ { a2: "Beers", a4: [ ...

How to make text in HTML and CSS stay at the bottom of a div?

I am in the process of creating a website dedicated to my "Starcraft II" clan. My goal is to have the navigation bar display the text "ALLOYE" and remain fixed at the bottom of the screen. I attempted to achieve this using the following code: vertical-alig ...