Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML Basic

HTML Reference

HTML oninvalid Attribute

Posted in HTML Tutorial
Updated on Nov 19, 2024
By Mari Selvan
👁️ 66 - Views
⏳ 4 mins
💬 1 Comment
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:

oninvalid.html
Copied
Copy To Clipboard
<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:

oninvalid.html
Copied
Copy To Clipboard
<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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (HTML oninvalid Attribute), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy