HTML URL type Property: URL Input Type

June 19, 2025

HTML URL type Property: URL Input Type Explained

The HTML <input> element with the type="url" property is specifically designed for input fields that should contain a URL (Uniform Resource Locator). When used, browsers automatically validate the entered text to ensure it conforms to a standard URL format. This helps in collecting correct and secure data from users, enhancing the overall user experience. This guide provides a detailed look at the URL input type, its attributes, and usage with practical examples.

What is the URL Input Type?

The <input type="url"> is an HTML5 addition, offering a specialized input field for URLs. It provides automatic validation, ensuring the entered text is a properly formatted URL. This not only improves data accuracy but also offers a better user experience by providing immediate feedback on input validity.

Purpose of the URL Input Type

The primary purposes of the URL input type are to:

  • Ensure that users enter valid URLs.
  • Provide a user-friendly input method for URLs, often with optimized keyboard layouts on mobile devices.
  • Enable automatic validation by the browser, reducing the need for custom JavaScript validation.

Syntax and Attributes

The basic syntax for using the URL input type is:

<input type="url" id="urlInput" name="website" />

Attributes Table

Here’s a table of attributes commonly associated with the <input type="url"> element:

Attribute Type Description
`type` String Specifies the type of the input element. For URL input, it is set to `”url”`.
`id` String A unique identifier for the input element, used to reference it in JavaScript or CSS.
`name` String Specifies the name of the input element, which is used when submitting the form data.
`value` String Specifies the initial value of the input field.
`placeholder` String Provides a hint to the user about what kind of data is expected in the input field.
`required` Boolean Specifies that the input field must be filled out before submitting the form.
`readonly` Boolean Specifies that the input field is read-only and cannot be modified by the user.
`size` Number Specifies the visible width of the input field in characters.
`pattern` String Specifies a regular expression that the input field’s value must match to be considered valid.
`autocomplete` String Specifies whether the browser should offer autocomplete suggestions for the input field.

Basic Examples

Let’s start with some basic examples of how to use the URL input type.

Basic URL Input

This example demonstrates a simple URL input field with a label.

<form>
  <label for="website">Enter your website URL:</label><br />
  <input type="url" id="website1" name="website" /><br /><br />
  <input type="submit" value="Submit" />
</form>

This code creates a basic form with a URL input field. The browser will automatically validate whether the entered text is a valid URL when the form is submitted.

URL Input with Placeholder

Adding a placeholder provides a hint to the user about the expected input format.

<form>
  <label for="website">Enter your website URL:</label><br />
  <input
    type="url"
    id="website2"
    name="website"
    placeholder="https://www.example.com"
  /><br /><br />
  <input type="submit" value="Submit" />
</form>

The placeholder text disappears when the user starts typing in the input field. 💡

Required URL Input

The required attribute ensures that the user must enter a value before submitting the form.

<form>
  <label for="website">Enter your website URL:</label><br />
  <input
    type="url"
    id="website3"
    name="website"
    required
    placeholder="https://www.example.com"
  /><br /><br />
  <input type="submit" value="Submit" />
</form>

If the user tries to submit the form without entering a URL, the browser will display an error message. ⚠️

Read-Only URL Input

The readonly attribute makes the input field uneditable.

<form>
  <label for="website">Enter your website URL:</label><br />
  <input
    type="url"
    id="website4"
    name="website"
    value="https://www.example.com"
    readonly
  /><br /><br />
  <input type="submit" value="Submit" />
</form>

The user cannot modify the value in a read-only input field. 🔒

URL Input with Size

The size attribute specifies the visible width of the input field.

<form>
  <label for="website">Enter your website URL:</label><br />
  <input
    type="url"
    id="website5"
    name="website"
    size="30"
    placeholder="https://www.example.com"
  /><br /><br />
  <input type="submit" value="Submit" />
</form>

The input field will be 30 characters wide. 📏

Advanced Examples

Let’s explore some advanced examples that demonstrate more complex uses of the URL input type.

URL Input with Custom Validation Pattern

You can use the pattern attribute to specify a regular expression that the URL must match.

<form>
  <label for="website">Enter your website URL:</label><br />
  <input
    type="url"
    id="website6"
    name="website"
    pattern="https://.*"
    placeholder="https://www.example.com"
    title="URL must start with https://"
  /><br /><br />
  <input type="submit" value="Submit" />
</form>

In this example, the URL must start with https://. The title attribute provides a helpful message to the user if the input doesn’t match the pattern. 📝

URL Input with JavaScript Validation

You can also use JavaScript to perform custom validation.

<form id="urlForm">
  <label for="website">Enter your website URL:</label><br />
  <input
    type="url"
    id="website7"
    name="website"
    placeholder="https://www.example.com"
  /><br /><br />
  <button type="button" onclick="validateURL()">Validate</button>
</form>

<script>
  function validateURL() {
    const urlInput = document.getElementById("website7");
    const urlValue = urlInput.value;

    try {
      new URL(urlValue);
      alert("Valid URL");
    } catch (error) {
      alert("Invalid URL");
    }
  }
</script>

This example uses the URL constructor in JavaScript to validate the URL. 🎉

Real-World Applications

The URL input type is useful in various scenarios:

  • Collecting Website Information: In user profiles or contact forms.
  • Validating API Endpoints: Ensuring the entered API endpoint is a valid URL.
  • Form Submissions: Any form that requires a URL, such as a link submission form.

Accessibility Considerations

  • Labels: Always use labels for URL input fields to provide context for users.
  • Error Messages: Provide clear and helpful error messages for invalid URLs.
  • ARIA Attributes: Use ARIA attributes to enhance accessibility for users with disabilities.

Browser Support

The <input type="url"> is supported by all modern browsers. 💯

Conclusion

The HTML URL input type is a valuable tool for web developers to ensure that users enter valid URLs in forms. By using the URL input type and its associated attributes, you can improve data accuracy and enhance the user experience. This guide has provided you with the knowledge and examples you need to effectively use the URL input type in your web projects. Happy coding!