HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML oninvalid Attribute
Photo Credit to CodeToFun
🙋 Introduction
The oninvalid
attribute is a powerful feature in HTML that allows developers to define a script to be executed when a form element's value is found to be invalid.
This attribute is especially useful for providing custom validation feedback to users during form submission.
🎯 Purpose of oninvalid
The primary purpose of the oninvalid
attribute is to execute a script when a form element's value is deemed invalid according to the constraints specified by other attributes such as required, min, max, etc.
This allows developers to create a more interactive and user-friendly form validation experience.
💎 Values
The oninvalid
attribute takes a JavaScript code block as its value. This code block typically contains the logic that should be executed when the associated form element's value is considered invalid. It can include functions, alerts, or other actions to provide feedback to the user.
📄 Example
Let's look at a simple example of how to use the oninvalid
attribute in an HTML form:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required oninvalid="alert('Username is required!');">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Submit">
</form>
🧠 How it Works
In this example, the oninvalid
attribute is applied to the username input field, and an alert is triggered if the field is left empty when the form is submitted.
🔄 Dynamic Values with JavaScript
Similar to other attributes, the oninvalid
attribute can be dynamically set or modified using JavaScript.
This is particularly useful when you want to adjust the validation behavior based on user interactions or specific conditions. Here's a quick example:
<script>
// Dynamically set oninvalid for an input field
document.getElementById("dynamicField").oninvalid = function() {
alert("This field is invalid. Please provide a valid input.");
};
</script>
🧠 How it Works
In this script, the oninvalid
attribute is dynamically set for an input field with the id dynamicField. The associated function displays an alert when the field is found to be invalid.
🏆 Best Practices
- Use the
oninvalid
attribute to provide meaningful feedback to users when form validation fails. - Keep the script within the
oninvalid
attribute concise and relevant to the validation error. - Test the form across different browsers to ensure consistent behavior.
🎉 Conclusion
The oninvalid
attribute enhances the validation capabilities of HTML forms, allowing developers to create a more interactive and user-friendly experience.
By understanding and implementing this attribute judiciously, you can improve the quality of form submissions on your website.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML oninvalid Attribute), please comment here. I will help you immediately.