Can a shake event be utilized to indicate necessary form fields?

Hey, so I'm working with this form and implementing validation using the onclick event and submitting on change.

<form action="reservation.php" method="post">
                             <input class="date" id="from" name="from" type="text" value="<?php echo $from; ?>" onclick="document.getElementById('to').removeAttribute('disabled')" >   
                             <h5>Checkout</h5>
                             <input class="date" id="to" name="to" type="text" value="<?php echo $to; ?>" disabled onclick="document.getElementById('room_type').removeAttribute('disabled')">
                             <h5>Room</h5>
                             <select id="room_type" name="room_type" class="frm-field required" onChange='this.form.submit()' disabled>
                               <option value="" selected="selected" disabled="disabled">Select</option>
                                    <option value="Delux">Deluxe Rooms</option>       
                                    <option value="Executive">Executive Rooms</option>
                                    <option value="Suite">Suites</option>
                              </select>
                              <noscript><input type="submit" name="submit" value="Check"/></noscript>
                              </form>   

If someone attempts to click directly on the Room select field, both the Checkin and Checkout fields will shake with a red border. However, if someone selects a value for the Checkin field but doesn't interact with the Checkout field before going to the select field, only the Checkout field will shake with a red border, indicating it is a required field.

Any help in achieving this would be greatly appreciated!

Answer №1

$( document ).click(function() {
  $( "#toggle" ).effect( "shake" );
});
#toggle {
    width: 100px;
    height: 100px;
    background: #ccc;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>shake demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
 
<p>Click anywhere to shake the box.</p>
<div id="toggle"></div>
 

</body>
</html>

When a click event is triggered with jQuery UI, you can apply the shake effect.

$( "#to" ).effect( "shake" ).css( "border", "2px solid red" );

EDIT

Another alternative is using animation css :-

<button id="shakebtn">shake</button>
<input class="date" id="to" name="to" type="text" value="<?php echo $to; ?>" disabled onclick="document.getElementById('room_type').removeAttribute('disabled')">

<script type="text/javascript">
    $('#shakebtn').click(function(){
       $('#to').removeClass().addClass('animated shake borderred').one('webkitAnimationEnd oAnimationEnd', function(){
            $(this).removeClass();
       });
    });
</script>

CSS styling:

.borderred {
    border:2px solid red
}

Based on your comment, I have included a demo from here. Please review for simple setup with JavaScript that fits within your code structure.

Answer №2

Implementing client-side validation is crucial to ensure data accuracy and user experience. In case of validation failure, it's important to trigger specific events to handle the errors smoothly.

$('#submitButton').on('click', function() {
  $( "#inputField" ).effect( "shake" ).css( "border", "2px solid red" );
});

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

Why is External Interface in Flash no longer supported in IE11?

Snippet Query: <object id="myMovie" name="myMovie" type="application/x-shockwave-flash" width="100%" height="100%" data="swf/myMovie.swf"> <param name="movie" value="swf/myMovie.swf" /> <param name="allowFullScreen" value="true" /&g ...

I'm experiencing an issue with the sub-menu disappearing and preventing me from clicking on any links

Currently, I have set up a navigation bar with a sub-menu that spans the full width of the page. When hovering over the list items, the sub-menu appears as expected. However, when moving the mouse down into the sub-menu, it disappears instantly. The object ...

A parameter value is needed for the Controller - Error 500: Internal server hiccup

Is integrating AJAX with Symfony2 really as challenging as it seems? I am aiming to create a single-page application that features a form for creating posts, each consisting of a title and content. Upon form submission, AJAX should handle displaying the p ...

Numbering updated on pagination

Is there a way to achieve this? I currently have a pagination script that pulls data from a mysql database via php and uses jquery for functionality. I am interested in incorporating a filter into the pagination system, allowing users to remove certain c ...

Elevate your website design with Bootstrap by placing a fixed horizontal navigation bar above all

I am facing an issue with my vertical Bootstrap sidemenu that transitions to horizontal layout responsively. When the sidemenu switches to a horizontal display, it ends up being hidden behind other elements, particularly on smaller devices. The example de ...

Utilizing JQuery to display JSON data on a data table using Ajax

Seeking guidance on jQuery, I am diving into a new datatable venture with preloaded data. Upon searching, the goal is to update the table by removing existing data and displaying only the search results. My attempt so far includes: app.common.genericAjaxC ...

What is the best way to eliminate the empty space at the bottom of my Bootstrap site?

Help needed with my Bootstrap webpage! I'm encountering an issue where there is a 5px gap at the bottom of my page that I can't seem to get rid of. I've tried adjusting the margins and padding to no avail. Any suggestions on how to fix this ...

Implementing Ajax with multiple div elements

I am currently working on an AJAX code that displays more details in one div. However, I would like to expand this functionality to show AJAX content in two or three divs simultaneously. How can I achieve this? function GetDetails(id) { $(document) ...

To prevent the background image (blue border) from shifting, I need to extract the list item element without causing the image to

Having trouble with the side borders on my navbar. I've used a background image for the border (blue line) in the list item of the navbar. Is there a way to move down two specific list items slightly, like shown in my screenshot? I need to bring down ...

What could be causing my jQuery to malfunction after I include the .sqrt() function?

Seeking assistance in creating a grid of divs, I am working on finding the square root of the user's input to determine the height and width required to form a square. The code below is what I currently have: $('#size').click(function(){ $ ...

The lack of vertical alignment in the table

Trying to do something really simple here and I'm a bit stuck. Currently working on a forum project, and surprisingly the backend is error-free (thanks to some questionable magic tricks), However, my current task is to display all subcategories using ...

Eliminate inner margin from the canvas of a bar chart created with ChartJS

I am looking to use Chart.js to create a single stacked bar chart that visualizes progress towards a specific financial goal. I have added a dotted line on top of the chart to represent this goal amount. The issue I am facing is that there is unwanted pad ...

Unable to change background color in iPhone layout

My attempt to change the header's background color is successful in desktop layout, but not working as expected on iPhone. The header color remains unchanged in the iPhone layout. Below is my code snippet: http://jsfiddle.net/mzMjT/embedded/result/ ...

Bootstrap 4: Arrange input fields in a horizontal layout

I'm a newbie when it comes to Bootstrap 4 and could use some guidance. I'm looking to have my first two input fields aligned horizontally on the same line, instead of stacked on top of each other. Something like this: check out the image here B ...

What is the best way to handle a quote within a double quotation mark?

I need assistance in adding a table with HTML code: string _table = @"{ ""version"": {""number"": 2}, ""type"":""page"", ...

Keep the span's width constant even when it is empty

Is there a way to preserve the width of an empty span element? Consider the following HTML pseudo table: <p><span class="col1">Day:</span><span class="col2"><?php echo somephp ?></span></p> <p><span clas ...

Is it possible for SESSION variables to have delays when being set or unset?

I've been curious about the speed at which a SESSION variable can be set or unset. I haven't been able to test it exactly, but I have noticed that when using an ajax call to set/unset a SESSION variable, about 10% of the time the code in the call ...

What is the best way to conceal floated elements with overflow in CSS?

How can I ensure that my block container element only displays the content of block elements, cutting off any floated elements that are taller than the parent container? Applying overflow: hidden seemed like a simple solution, but it created a new block f ...

Transforming the text color after clicking on it on Squarespace - here's how to do it!

How can I make text change color upon clicking in Squarespace? Where should I place the block id in the code to target a specific block? Check out the code snippet I've been experimenting with: <script> document.getElementById('chang ...

Loop through 3 items in jQuery and repeat each item by decrementing it

I need to develop a jQuery function that will increment or decrement a value every 3 seconds using the window.interval method. Here is the desired sequence: 1st: 1, 2, 3 After 3 seconds 2nd: 3, 1, 2 After 3 seconds 3rd: 2, 3, 1 After 3 seconds 4th: 1, 2, ...