Delay HTML5 :invalid pseudo-class until the first event
http://www.alistapart.com/articles/forward-thinking-form-validation/
Since we only want to denote that a field is invalid once it has
focus, we use the focus pseudo-class to trigger the invalid styling.
(Naturally, flagging all required fields as invalid from the start
would be a poor design choice.)
Following this logic, your code would look something like this...
<style>
input:focus:required:invalid {background-color: pink; color: white;}
input:required:valid {background-color: white; color: black; }
</style>
Created a fiddle here: http://jsfiddle.net/tbERP/
As you'd guess, and as you'll see from the fiddle, this technique only shows the validation styling when the element has focus. As soon as you move focus off, the styling is dropped, regardless of whether it is valid or not. Not ideal by any means.
Validation in HTML5. :invalid classe after submit
No there is nothing out of the box.
Mozilla has its own pseudoclass for something very similiar called ':-moz-ui-invalid'. If you want to achieve something like this, you have to use the constraint validation DOM-API:
if(document.addEventListener){
document.addEventListener('invalid', function(e){
e.target.className += ' invalid';
}, true);
}
You can also use webshims lib polyfill, which will not only polyfill incapable browsers, but also adds something similiar like -moz-ui-invalid to all browser (.form-ui-invalid).
How to make pseudo class :invalid apply to an input AFTER submitting a form
You can add a class to the form when the submit-button was clicked and adjust your CSS selector so it only matches the input fields in a submitted form:
document.getElementById("submitButton").addEventListener("click", function(){
document.getElementById("testForm").className="submitted";
});
form.submitted input:invalid{
border:1px solid red;
}
<form id="testForm">
<input type="text" required>
<input type="submit" value="submit" id="submitButton">
</form>
How to work input[type=text]:invalid with html 5 required?
Can you try below code? here It will have red border only when you focus input. However If you wish to display border when focus is lost after focusing once then you may need to write some javascript code instead of css.
input:focus:invalid {border: 1px red solid;}
Delay HTML5 :invalid pseudo-class until the first event
http://www.alistapart.com/articles/forward-thinking-form-validation/
Since we only want to denote that a field is invalid once it has
focus, we use the focus pseudo-class to trigger the invalid styling.
(Naturally, flagging all required fields as invalid from the start
would be a poor design choice.)
Following this logic, your code would look something like this...
<style>
input:focus:required:invalid {background-color: pink; color: white;}
input:required:valid {background-color: white; color: black; }
</style>
Created a fiddle here: http://jsfiddle.net/tbERP/
As you'd guess, and as you'll see from the fiddle, this technique only shows the validation styling when the element has focus. As soon as you move focus off, the styling is dropped, regardless of whether it is valid or not. Not ideal by any means.
How to have onclick() event as well as HTML5 form validation together?
This has nothing to do with the onclick event. It is caused by the alert
.
You are pulling the focus away from the form which causes the browser generated error message to be removed at the instant it would normally be displayed.
Don't use alert()
. Instead, modify the DOM of the page to display the message there.
Related Topics
Pure CSS Continuous Horizontal Text Scroll Without Break
Html: How to Force Links to Open in a New Tab, Not New Window
How to Use the CSS Pseudo-Element :Before{ Content: '' } to Affect an <Option> Element
How to Use the Z-Index Properly
Three Divs Side by Side with Spacing in Between
Equal Width Using Flex and Border-Box
How to Disable All Action Buttons While Shiny Is Busy and Loading Text Is Displayed
Coloring the Text Depending on Numeric Value Using CSS
HTML Generated Microsoft Word Document with Header, Footer and Watermark
How to Change the Font-Size of an <Option> Element Within <Select>
How to Create a Slanted Background with CSS
Chrome: Automatic Shift of Web Elements
Customise Ng Bootstrap Carousel in Angular
Css: Borders with Negative Radius