Utilize Bootstrap's empty field validation for various other validation purposes

After downloading a bootstrap theme for my website, I noticed an elegant validation feature for required fields. When a field is left empty, a red exclamation mark appears behind the input, and hovering over it reveals the error message inside the field.

The HTML structure looks like this:

<div class="wrap-input100 validate-input m-b-10" data-validate = "Username is required">
  <input class="input100" type="text" name="username" placeholder="Username">
  <span class="focus-input100"></span>
  <span class="symbol-input100">
  <i class="fa fa-user"></i>
  </span>
</div>

The associated CSS code is:

.validate-input {
  position: relative;
}

.alert-validate::before {
  content: attr(data-validate);
  /* other CSS properties */
  visibility: hidden;
  opacity: 0;
  /* transition effects */
}

.alert-validate:hover:before {
  visibility: visible;
  opacity: 1;
}

@media (max-width: 992px) {
   .alert-validate::before {
    visibility: visible;
    opacity: 1;
  }
}

And here is the JQuery function included:

(function ($) {
"use strict";

// Validate function to check input fields

})(jQuery);

I'm looking to expand this validation system to cover various scenarios such as character limits for usernames or specific requirements for passwords. However, duplicating the same HTML, CSS, and jQuery code with minor modifications seems inefficient. Could you guide me on how to approach this challenge effectively?

Appreciate any help or suggestions!

Answer №1

In your validate function, you have the flexibility to include another condition using an additional if statement. For instance:

if( $(input).attr('type') == 'text' && $(input).val().length > 10 ) {
  return false;
}

This snippet will perform the following checks:

  • Determine if the input type is 'text'
  • Validate whether the length of the value exceeds 10 characters

To verify if a string or 'password' contains special characters, you can utilize the following code:

if( $(input).attr('type') == 'password' && /^[a-zA-Z0-9- ]*$/.test($(input).val()) != false) {
  return false;
}

Similar to before, these conditions will:

  • Confirm that the input type is 'password'
  • Check if the value consists solely of lowercase letters, uppercase letters, and digits (Regular Expressions are immensely helpful for validation)

The possibilities are endless depending on your creativity and specific requirements :)

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

using the image source in the onclick function

I have implemented the following code to convert an image to grayscale using Javascript. <body> <canvas id="myCanvas" width="578" height="400"></canvas> <script> function drawImage(imageObj) { var canvas = document.getElement ...

What is the best way to adjust the size of an SVG image using the :before pseudo-class?

Is there a way to display an image in a CSS :before element? .withimage:before { content: url(path/to/image.svg); display: block; height: 20px; width: 20px; } I'm facing an issue where the height and width properties are applied to the eleme ...

Assistance needed in handling a form with the use of $_POST and $PHP_SELF

Let me explain my goal in creating a webpage using HTML and PHP. The webpage will feature multiple forms to gather user inputs for future use. My plan is to reveal each form only after the previous one has been submitted. Here are the 3 forms: First name ...

Insert values for each option selected in the multiple selection

In my form, I have multiple select options. My goal is to insert a value into the database for each user who is selected with a checkbox, based on the options chosen in the select menu (which allows multiple selections). User1: 5,7,8 User2: 6,4 User ...

Why is it not possible for me to choose an element after it has been placed within a grid component?

Struggling to eliminate the blur effect on an image inside a grid element? It seems modifying the properties of an element within a grid class is causing some issues. To simplify, I conducted a basic test: I managed to change the ppp2 class when hovering ...

When a textarea contains a new line, it will automatically add an additional row and expand the size of the textarea

Developed a form with the functionality to move placeholders in a textarea upwards when clicked. The goal is to increment the height of the textarea by 1 row for every line break. However, I am uncertain about what to include in the if statement of my Ja ...

What is the process for transferring information from a Ruby controller to an application JavaScript using AJAX?

When clicking a button, an AJAX call is made in my application.js file. It sends 3 data points to the events_controller#check action: //application.js $(document).on('click', "#check-button", function(){ ... $.ajax({ ...

Looking to authenticate a CSS span class within an XML document using Oxygen Program?

This is in connection with my initial query. The XML does not recognize the CSS span class. <l rend="indent"> <span class="face_dropcap_ ">C</span><hi rend="initial_roman">E</hi>stoit alors que le prefent des <span class= ...

Tips for creating a responsive div that contains both an image and description, including automatically resizing the image to fit the

#content { background-color: #ACAE4C; float: right; display: inline-block; padding: 0; } <div id="content"> <div class="pic"></div> <div class="desc"></div> </div> I need assistan ...

Arrange the inline-block elements at the bottom of a div

Hey there! I'm currently working on designing a website header using bootstrap 3, which consists of four main divs. Within each of these divs, there are two nested divs. Imagine one of the initial divs as shown below: _______________________________ ...

Deactivate the other RadioButtons within an Asp.net RadioButtonList when one of them is chosen

Is there a way to disable other asp.net radio buttons when one of them is selected? I have three radio buttons, and I want to disable the other two when one is selected. After doing some research, I managed to disable the other two when the third one is se ...

PHP script output continuously responded with AJAX

Encountering an issue with the AJAX response from my PHP script... I've set up a "Status" div to display the response of the PHP script. It currently only shows the response once the entire script has finished, but I would like it to show each echo " ...

What are the steps to insert a plotly graph into a webpage so that it responds to different

I have been working on integrating a plotly pie chart into my website. In order to make the iframe responsive, I decided to utilize this specific tool to generate the necessary html/css. While the chart appears satisfactory on a laptop screen, it is barely ...

The feature of option display is not supported on Safari browser

I am facing an issue with the functionality of two dropdown menus. The options in the second dropdown are supposed to be shown based on the selection made in the first dropdown. While this feature works perfectly in Chrome, it seems to be malfunctioning i ...

CSS: Changing border color based on current page

To achieve the desired effect, I need to change the border color to red when a specific page is active. When the phone option is clicked, the border should stay red consistently on that same page, and when the email option is clicked, the border should t ...

"Enhance the functionality of multiple elements with this innovative Jquery

Exploring the realm of Jquery plugins, I am faced with the challenge of making my plugin compatible with multiple elements. After scouring tutorials and various online forums, I've come to realize that plugin development is heavily dependent on the sp ...

Encountering an "Undefined error for $" in WordPress while trying to implement the Graph Library for J

I successfully integrated a Graphing and Plotting Jquery library called JQplot into my WordPress site by adding the following code to my functions.php file: function rw_jqplot() { // JS wp_deregister_script('jqplot'); wp_enqueue_scri ...

Type within a customizable div element located within an iframe

When I switched to using iframe driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='ContentFrame']"))); I attempted two different methods, but neither of them worked. Approach 1: WebElement webElement = driver.findElement(locat ...

Leveraging Generic Types in React with TypeScript for Dynamically Assigning HTML Props based on Element Tags

I am frequently in need of a component that is essentially just a styled HTML tag. A good example is when I want to create beautifully styled div elements: // Definitions const styledDiv = (className: string) => { const StyledDiv = React.FC<HTMLA ...

Iterate through the JSON response once the AJAX call is successful

Apologies for the duplication, this question was originally asked on Stack Overflow, but as a newcomer to this topic, I am seeking guidance on how to achieve it? Below is my AJAX call: $("#btnprocess").click(function () { $.ajax({ ...