Introduction to utilizing the features of HTML5 tag attributes and HTML5+CSS form validation within Struts2 tag elements

We currently have a heavy reliance on struts2 in our application. One of the challenges we are facing is to eliminate all default struts2 validation and implement HTML5 with CSS validation instead.

For example, when working with a struts2 form:

<s:form id="userForm" action="userAction" method="post" validate="true">
   <s:password key="Pwd" required="true" maxlength="128" />
 </s:form>

In this scenario, I aim to utilize HTML5 attributes such as "type=password", "required", and pattern="(?=.\d)(?=.[a-zA-Z])\w{7,}", while also implementing CSS validation for these elements. Can anyone provide guidance or a tutorial on how to achieve this? Your help would be greatly appreciated.

I specifically want to address the issues I am encountering with CSS validation.

Below is an example of my struts form:

<s:form id="userForm" action="userAction" method="post" validate="true">
       <s:password key="Pwd" type="email" required="true" maxlength="128" id="password"             pattern="(?=.*\d)(?=.*[a-zA-Z])\w{7,}"/>
       <span class="form_hint">Password should contain,<br> 
                1.minimum of 7 characters. <br> 
                2. Must consist of only letters and digits.<br> 
                3.At least one letter and at least one digit</span>
       </s:submit type="button"> 
     </s:form>

I am looking to validate the password field for: 1. Ensuring it is filled out before submission. 2. Meeting the specified pattern criteria, turning the text green if successful.

My CSS code looks like this:

.form_hint {
        background: #d45252;
        border-radius: 3px;
        color: white;
        margin-left: 8px;
        padding: 1px 6px;
        z-index: 999; 
        position: absolute;
        display: none;
    }

    .form_hint::before {
        content: "\25C0"; 
        color: #d45252;
        position: absolute;
        top: 1px;
        left: -6px;
    }
    #emailId input:focus + .form_hint {
        display: inline;
    }

    #emailId input:required:valid + .form_hint { 
        background: #28921f;
    }

    #emailId input:required:valid + .form_hint::before {
        color: #28921f;
    }

However, when testing the form, only the HTML5 pattern tag attribute seems to work, displaying a message saying "Please enter required format". How can I make my CSS styling effective? What syntax should I use in my CSS file? The selectors "#emailId input:focus + .form_hint", "#emailId input:required:valid + .form_hint", "#emailId input:required:valid + .form_hint::before" do not seem to apply in my form.

Answer №1

I recently integrated jquery validation-engine with struts tags to add CSS validations, and I successfully incorporated it into my project.

If you're interested in the tutorial I followed for using jquery-validation engine, you can check it out here:

Here's a snippet of how my struts tags are set up:

  <s:textfield name="tenant.shortName" label="Short Name" labelSeparator=" " id="shortName" maxlength="3"
                    cssClass="validate[required,custom[noSpecialCaracters]] text-input"></s:textfield>

EDIT If you prefer using HTML5, there is a solution available:

How do I specify HTML5 attributes with Struts 2.x? ...

For adding CSS, simply use cssClass="" within your struts tags to apply your desired styles.

Please note that a span element may not work inside <s:form> as it will be rendered as tables when expanded by the taglib, making it inaccessible near the password field in the final HTML output.

One suggestion: Utilize the title attribute to display additional information on mouse hover or consider using the tooltip attribute within <s> tags for a similar effect.

Answer №2

It appears that you are utilizing the default XHTML Theme, which automatically includes extra HTML elements like the <fieldset>.

Here's what you can do:

  1. Switch to the Simple Theme for more control over the generated HTML,
  2. Alternatively, if your fields are not adjacent anymore, consider using the ~ (General Sibling Combinator) instead of the + (Adjacent Sibling Combinator).

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

What steps do I need to follow to change the background color to white for the <App/> component in solid-js?

In my solid-js application styled with tailwindcss, I established a gray color in the index.css for the background of various screens. However, I wanted the main <App/> component to have a white background instead. To achieve this, I created an App.c ...

What is the best way to implement this component into my vue.js project?

I am having trouble integrating this vue component into my app as it is not loading properly. You can find the source of the component here. The following warnings are showing up: Unresolved function or method isNumeric() at line 35 Unused parameter e at ...

Experience the magic of Materialize CSS SideNav

I'm currently attempting to implement the mobile side navigation provided by Materialize. I've managed to display the menu icon, but when I click on it, nothing happens. It seems like the function is not being triggered, and there are no errors s ...

Achieving a centered SearchBar with a single button on the NativeBase header

I am trying to center the search bar with the menu button on the right side of the header using Nativebase. The code for this is provided below: <Header> <Body> <Button style={styles.searchButton} onPress={() => ...

A step-by-step guide on incorporating RAW css into your Jekyll website

I am experiencing an issue with loading a CSS file on my website. <link rel="stylesheet" href="/assets/css/my_file.css"> This file is located at _assets/css/my_file.css. However, when I try to load the page, the CSS file does no ...

Proper method for positioning text in a line

Trying to recreate the image below, but facing alignment issues with the text in my code. How can I vertically align the text so that they are aligned like in the photo? Flexbox hasn't helped due to varying text lengths causing misalignment. const ...

What exactly is the significance of "hash" in terms of Internet Explorer style

After downloading a sample CSS file, I noticed the following entry: #text-align: right; The comment beside the entry mentioned that this expression is specifically for justifying it in IE, while text-align: right; works for Safari and Chrome. My questi ...

Utilize custom scrollbar design exclusively for Windows operating system

My goal is to customize the scrollbar style of a div specifically for Windows systems. While most mobile devices have invisible scrollbars that I want to keep hidden, the aesthetics of Mac OS scrollbars are acceptable to me. What I really need is to make t ...

JQuery selecting and highlighting a row within a dynamically loaded table via ajax

On a webpage, there are two tables being displayed. The first table loads along with the entire page, while the second table is loaded later on using ajax. Each row in both tables contains links. Upon clicking a link, the corresponding row in the table ...

The Element fails to go back to its original position due to CSS Float

After changing the screen resolution, it appears that the nav-search-ul element fails to return to its correct position. It seems like the media query is not working properly. This issue only occurs in Chrome, as everything works fine in Firefox and IE. ...

Exporting image to excel using HTML code

I am working on a unique project where I have successfully converted an image through canvas object and placed it within a div tag, which is nested in a table tag. My goal now is to export this table to Excel using ColdFusion. Below is the code snippet tha ...

Is it possible to utilize the identical Thymeleaf fragment modal on a single page, albeit with varying parameters?

I'm experiencing an issue with my website where I have cards with different headlines and content. When I click on a card, a modal should open with the specific text from that card. However, the problem is that the parameters from the first card are a ...

Modify the href value by matching it with the parent div attribute

I am working on an HTML project <div class="1" style="" title="NeedthisText TextIDontneed"> <div class="2> <div class="3"> <a target="_blank" href="/NeedToChange/DispForm.aspx?ID=1"></a> </div> </div> &l ...

Incorporating JSTree Into Your Website's Heading: A Step-By-Step

I'm in search of a way to integrate the following code snippet as a JSTree into the heading section of a webpage, depicted below: <div id="jstree"> <ul> <li>Core 1 <ul> & ...

I am experiencing an issue with my Angular directive where the button click does not

Check out this page for guidance on adding a div with a button click in AngularJS: How to add div with a button click in angularJs I attempted to create an angular directive that should add a div to my HTML page when a button is clicked. However, upon cli ...

Implementing a function in ReactJS that is activated by multiple buttons

Apologies for the unclear title. My issue revolves around having three different button tags, each of which should send a unique argument to the function selectSupplier(). However, no matter which button is clicked, only the last value ("ultramar") is be ...

In regards to navigational bars that are oriented horizontally

Trying to create a simple navigation bar with horizontal links but facing issues. Can't seem to make it work despite following examples online: <!-- Navigator --> <div id="nav"> <ul> <li><a href="#"& ...

Achieve perfect alignment of an HTML table by positioning it precisely 36 pixels away from the right edge of the window

I'm struggling to achieve a table that is precisely 36px away from both the left and right edges of the browser window, regardless of CSS. While I can easily set the left margin, getting the exact right margin seems impossible as the `margin-right` CS ...

Implementing conditional ng-show based on checkbox status in AngularJS

I have created this code to filter out the out-of-stock products using ng-show. When the checkbox is checked, only the available products should be displayed. HTML: <input type="checkbox" id="chkStock" value="Exclude Out of Stock" ng-model="exclude" / ...

I am having an issue with a mobile JQuery collapsible div that is not properly displaying the internal div within the item

Mobile JQuery jquery.mobile-1.0rc2.min.js HTML 5 Asp.net 4.0 I am having trouble with a gridview item-template where I have implemented the collapsible div from JQuery Mobile. The label text within the h3 element can be quite long, and I want to clip it i ...