Modify CSS and animation through jQuery with ASP.NET Field Validator

My current form contains around 10 controls, similar to the one below:

<asp:TextBox ID="fullname" Width="200" MaxLength="50" runat="server"/>
<asp:RequiredFieldValidator Display="Dynamic" ValidationGroup="contact" ControlToValidate="fullname" ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"/>

I appreciate how ASP.NET fieldvalidators function, but I would like to customize the way validation errors are displayed. Instead of showing an asterisk when validation fails, I want to highlight the input field in red.

To achieve this, I intend to use jQuery for more control over CSS and animations. I prefer this method over ASP.NET animations, which I find overly complex.

I came across a post on Stack Overflow that discussed a similar issue (Change Text Box Color using Required Field Validator. No Extender Controls Please), but it did not provide a clear solution.

So, my question is: Is there a way to retain my ASP.NET validator controls while changing the way error messages are displayed? Can I use jQuery to add or remove animations and CSS classes to the validated input fields instead?

Requirements:
- The code should work for requiredfieldvalidator and regularexpressionvalidator, among others.
- It should capture the enter key press event.
- Error CSS should be removed when the user corrects the input field and validation succeeds.

Thank you!

Answer №1

Incorporate Custom Validator In Your Code

Here is how you can set it up in your aspx file:

  <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
                <asp:CustomValidator ID="cvFirstName" runat="server" SetFocusOnError="true" Display="Dynamic"
                    ValidateEmptyText="true" ControlToValidate="txtFirstName" ClientValidationFunction="validateValue"></asp:CustomValidator>  

Next, include the following JavaScript logic:

   <script type="text/javascript">
        function validateValue(source, args) {
            if (args.Value == "") {
                args.IsValid = false;
                document.getElementById(source.id.replace('cv', 'txt')).className = 'Invalid';                
            }
            else {
                args.IsValid = true;
                document.getElementById(source.id.replace('cv', 'txt')).className = 'Valid';
            }
        }  
    </script>

And finally, style it using CSS:

 <style type="text/css">
        .Invalid
        {
            border: 1px solid red;
        }
        .Valid
        {
            border: 1px solid White;
        }

    </style>

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

Creating Buttons with Multiple Functions

Hey there, I've been working on some code... It's functional, but it's not quite meeting my expectations. $('.yesOrNo').on('click', function() { var $yes = $("#yes").on('click'); var $no = $("#no").on(&a ...

Creating a custom design for input outline borders using CSS3

My CSS3 code is structured as follows: input[type="text"], input[type="date"], textarea, input[type="radio"], input[type="number"], input[type="time"], input[type="password"] { background: #ffffff; /* Old browsers */ /* IE9 SVG, ne ...

Ways to avoid the default values from being applied

I have included a demo below for your reference There are two default options values: usa and asia. Initially, I had to add a placeholder to both columns by default. Furthermore, I needed the placeholder to be unique for each tag. How can I accomplish t ...

Locate the initial div with a distinct class from the bottom upwards

If I have a HTML page and want to search for the last occurrence of a div with a specific class, how can I do that using Jquery? Assuming the following HTML structure: <div class="container"> <div id="1" class="something special"></div& ...

Iterate through the loop to add data to the table

Looking for a way to append table data stored in local storage, I attempted to use a loop to add cells while changing the numbers based on row counts. Here is my JavaScript code snippet: $(function() { $("#loadTask").change(function() { var ta ...

Modifying selections in the secondary dropdown menu based on the user's input in the primary dropdown menu

Thank you for taking the time to review this issue. I am working with two drop-down menus. The first one contains a list of clients, and the second displays projects associated with the selected client. Each project is linked to only one client. Therefor ...

Creating a toggle button with just HTML and CSS on a single page, no need for JavaScript!

I am a beginner attempting to merge HTML and CSS because the platform I'm using (getresponse) doesn't allow the uploading of separate .css files. It seems like I must be doing something incorrectly, but here is the HTML code: #cont { display ...

Choosing the right framework for a web application

Currently, I am at a crossroads when it comes to deciding on the architecture of the web application I will be developing. As part of a small team, I am tasked with working on this project solo while my colleagues focus on other tasks. The front-end of th ...

Overlap one element entirely with another

Currently, I am working on a way for an element called overlayElement to completely cover another element known as originalElement. The overlayElement is an iFrame, but that detail may not be significant in this scenario. My goal is to ensure that the over ...

Finding your way to a <div data-role="page"> sans the use of an anchor tag

I need assistance with a specific HTML setup. The first div tag in the code below contains a form with a table that has two columns labeled A and B, along with a button. The second div tag holds a text box, dropdown menu, and submit button. When the button ...

When you hover over the vertical sidebar menu in Bootstrap material design, a submenu will appear

Looking to create a three-column display that functions as one submenu when hovering over each menu item? <div class="container"> <div class="row"> <div class="col-sm-4"> <ul class="vertical-nav"> <li>&l ...

Make a jQuery request similar to using Postman

When attempting to send POST methods from my JavaScript file, I encountered an issue where the script did not work as expected. Below is a snippet of my HTML file: <html> <head> <script src="https://code.jquery.com/jquery-1.9.1.min. ...

Utilizing CSS and JavaScript for Image Masking

Is it possible to create a div that takes on the shape shown in this image and then fill it with an image? Alternatively, is there a way to transform an image into that particular shape using CSS or Javascript? Additionally, is it possible to arrange m ...

What method is being used by this website to carry out this specific task?

I recently came across this website, , and I was intrigued by the functionality of the matrix entry feature. By clicking on the bracket icon next to the H2O icon, you can access it. Upon accessing the matrix entry function, users are prompted to input th ...

A double click is required to enable the connected mouse press/release action

I have a section of code that is giving me some trouble: $('.toggle_box').each(function (){ var timeout, longtouch; $(this).bind('mousedown', function() { timeout = setTimeout(function() { longtouch = tru ...

Retrieve the initial index from a sorted array in JavaScript using an array

'I am looking to organize an array in numerical order, but I also need to be able to track the original index of each element after sorting. Consider the initial array: ptsGP = [3,8,2,5,6,9,8,4] Below is the code I am currently using to sort the ar ...

External JavaScript script files are causing issues, whereas internal scripts are functioning properly

Currently, I am working on incorporating JavaScript validations for mandatory fields within a form located in an ASP.NET Web Application. The process involves obtaining the ClientID of the control that requires validation, extracting its value, and checki ...

JavaScript: Navigating Through Frames and iFrames in Internet Explorer

Is there a way to run JavaScript code after my iFrames or Frames have finished loading? It seems like document.onReady fires before the frames are actually displayed on the page. Any ideas on how to make this work? I came across a plugin called FrameReady ...

Biodegradable AJAX jQuery Pagination

Seeking a way to implement AJAX-driven pagination in JQuery for my site with multiple pages of content. Want the user experience to involve seamless loading of new page content without refreshing the entire page. Considering how to ensure non-Javascript e ...

Integrate HTML forms seamlessly with Bootstrap

Currently, I am facing an issue while trying to align 2 forms side by side using the Bootstrap grid system. Despite following the documentation exactly, it doesn't seem to work and I can't figure out why. Here is the HTML code I'm using: ...