Checking the validity of an input element with jQuery: How to determine if the valid or invalid pseudo-class has been applied?

Is there a way to check for the application of the CSS3 :valid or :invalid pseudo-class on an input element using jQuery? I want to verify if the form element has passed CSS validation before allowing the submit button to be enabled.

Here are some methods I have attempted that proved unsuccessful:

if($("#el").is(":valid")) {
    //...
}

if($("#el:valid").length == 0) {
    //...
}

$("#el").attr("valid")

Answer №1

Following the update to jquery 1.10.2, I have verifed that utilizing the :valid selector functions as intended.

if($("#element").is(":valid")) {
    //...
}

Answer №2

For Jquery versions older than 1.10.2, you can handle it in the following manner:

if ($('#subscriberNumTb:valid').length > 0) {
    //add your code here
}

Answer №3

If you're interested in learning how to achieve this using pure JavaScript.

inputElement.checkValidity() // Returns `true` if valid, `false` if invalid

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

Obtain information from YouTube and format it into a list item

Currently, I'm working on compiling a list of videos that includes the title, link, image, and creator of each video. It's been a bit challenging :S <script type="text/javascript"> $(document).ready(function(){ $.getJSON('http://gdata ...

Guide to implementing a slider in HTML to adjust the size of my canvas brush

Hey there, I'm looking to implement a slider functionality under my canvas that would allow users to change the brush size. Unfortunately, all my attempts so far have been unsuccessful. Can anyone lend a hand? Much appreciated! <canvas i ...

Activate form caching in HTML/PHP

Feeling a bit overwhelmed trying to solve what seems like a simple issue. I have a collection of reports on an internal network (not accessible from the outside) and each report includes a form with various HTML inputs that adjust the report data. The pr ...

Send your information to a JSONP endpoint

Can data be posted to JsonP instead of passing it in the querystring as a GET request? I have a large amount of data that needs to be sent to a cross-domain service, and sending it via the querystring is not feasible due to its size. What other alternati ...

What is the best way to incorporate CSS into an HTML document?

I have a well-functioning invoice. When I save the CSS and HTML files in the same folder, the printed invoice looks good. Within the .html file, there are the following lines: <link rel="stylesheet" href="https://s3.amazonaws.com/appforest_uf/f1527345 ...

Having trouble with the functionality of a simple jQuery toggle menu on mobile?

I am experiencing an issue with a simple toggle menu that utilizes jQuery's on: tap feature, but it is not functioning as expected: <nav id="mobile-nav"> <ul> <li>Item 1</li> <li>Item 2</li> ...

Using Bootstrap's fourth version grid system

Struggling with creating a layout on bootstrap4? Here's a code snippet to help you out. Thank you in advance! <div class="col-8"> <a href="#"> <div class="card"> <img class="card-img" src="img.jpg"> ...

"Exploring the process of making a REST call from an Angular TypeScript client to

I'm currently developing a Sessions Server for a project at work. My dilemma lies in the fact that I'm struggling to find resources on how to make JavaScript HTTP calls from a server running with http.createServer() and server.listen(8080, ...) ...

"Effortlessly Load Content in Drupal7 Using jQuery AJAX without Refreshing the

I am currently in the process of getting an AJAX script to function properly on my webpage, which is built using Drupal 7. The objective is for the content of the page to load when one of the menu links is clicked, without refreshing the entire page (as th ...

Leverage TypeScript generics to link props with state in a React class-based component

Can the state type be determined based on the prop type that is passed in? type BarProps = { availableOptions: any[] } type BarState = { selectedOption: any } export default class Bar extends React.Component<BarProps, BarState> { ...

Aligning the last element within a list

I'm having a major issue with this code. Despite reading all related posts, I still can't get it to work. Seems like I might be missing something obvious. LOL. Here is the site (best viewed in Chrome): and here is what I believe is the simplifi ...

Using jQuery to Organize JSON Tables

I'm in a bit of a bind... I'm in need of sorting tables from a list of formulas. I've extracted 3 parameters from JSON files and created formulas, but now I need to sort this list of formulas. JSON files: <body> <script> $ ...

Tips for ensuring Marketo forms are responsive when integrated into a WordPress website

We are currently using Wordpress for our responsive website. Within the site, we have integrated Marketo forms (a marketing automation system) with custom CSS for styling. The issue we are facing is that while the forms appear fine on desktops, they cause ...

When attempting to insert data into MongoDB with node.js, an issue arises: "TypeError: Unable to access property 'username' of undefined."

I recently set up a MongoDB database and attempted to add data via a POST request using the Postman application. Upon successful creation of the database, a confirmation message was displayed. Here is the code snippet I used: users.js file: const router = ...

Can you explain the distinction between using `new ObjectId()`, `new ObjectId`, and `ObjectId()` in

Consider this initial definition in a file: const ObjectId = mongoose.Types.ObjectId; Which method should you choose and why? // 1 new ObjectId; // 2 new ObjectId(); // 3 ObjectId(); The official documentation recommends using new ObjectId. Person ...

Divide HTML elements every two words

Can you use CSS to break up HTML content after every 2 words, ensuring it works for any word combination? Example: // Original HTML content The cat is sleeping // Desired result: The cat is sleeping ...

Creating Dynamic Videos with PHP and HTML

Trying to display the user-submitted video, but all my attempts failed for some reason: 1. echo "<video width='500px'>"; echo "<source type='video/mp4' src=$var autoplay>"; echo "</video>"; echo "<video width=& ...

The inline script in JQuery 3.5.1 was refused to execute due to violating a Content Security Policy directive

After upgrading jQuery from version 2.1.1 to 3.5.1, I encountered an issue related to Content Security Policy directives. The inline script execution was refused due to a violation of the following Content Security Policy directive: "script-src 'sel ...

Using AJAX to search through paginated pages is quite simple and effective

Currently, I have developed a JavaScript script that filters names within a list. However, a problem has arisen with pagination - as the list paginates, I am unable to retrieve names without refreshing the page. Is there a way to use AJAX to access and dis ...

I am feeling uncertain about the most effective approach to utilizing ng-options in my current scenario

I've been grappling with a decision on how to proceed. I have an array of objects structured like this: $scope.assessments = [ { "name": "Goldman-Fristoe Test of Articulation - 2nd Edition", "description": "Description of Goldman-Fri ...