Definition and Usage
The type="url" attribute of the <input> tag represents a single-line URL input field.
This field is very similar to the basic single-line text field type, type="text", but differs in that it explicitly indicates that the input is intended for a URL.
Although it looks like a standard text input,
some devices with dynamic virtual keypads (such as mobile devices) support a keypad that helps with URL input, including keys for entering characters such as / and ., as well as keys for quickly entering strings commonly found in domain names, such as www. or .com.
Basic Example
<label for="user-url">Website URL</label>
<input type="url" id="user-url">
Basic Validation
When used inside a <form> tag, the <input type="url"> element is automatically validated by the browser.
This is a built-in feature of HTML5 that automatically checks whether the user has entered a correctly formatted URL.
<form>
<label for="user-url">Website URL</label>
<input type="url" id="user-url">
<button type="submit">Submit</button>
</form>
The <input type="url"> element in HTML5 performs basic format checks when validating URLs, but it does not perform highly detailed validation or perfectly cover every possible URL format. While behavior may vary depending on the browser, basic validation typically checks whether the value includes a protocol such as http:// or https://. However, validation may still pass if no value is entered or if the domain is omitted from the URL.
The following example demonstrates using the pattern attribute to explicitly require a protocol such as http:// or https://, along with the required attribute to make input mandatory.
Note
The URL format validation covered below is a simple example. It may have limitations in terms of accuracy and extensibility; therefore, its accuracy and extensibility cannot be guaranteed or warranted. Before relying on this example, you should carefully test and review it at your own discretion to ensure it is free of errors, bugs, or vulnerabilities.
<form>
<label for="user-url">Website URL</label>
<input type="url" id="user-url" required pattern="https?://.+">
<button type="submit">Submit</button>
</form>
This enables more precise validation for the URL input field.
Removing Built-in Basic Validation
To remove the validation provided as a built-in HTML5 feature, add the novalidate attribute to the <form> tag. This is a Boolean attribute. You only need to include the attribute itself without specifying a value.
<form novalidate>
<label for="user-url">Website URL</label>
<input type="url" id="user-url">
<button type="submit">Submit</button>
</form>
This removes the built-in validation.
Using Related Attributes
Let's learn about the attributes related to <input type="url">.
placeholder Attribute
The placeholder attribute specifies the text displayed in a text input control when no value is set. This is called placeholder text.
This value should provide the user with a brief hint about the expected type of data to enter into the control.
When the user starts entering text, the placeholder text disappears from the text input field.
<label for="user-url">Website URL</label>
<input type="url" id="user-url" placeholder="https://example.com">
For information on styling placeholder text, see Styling CSS Placeholder Text.
pattern Attribute
The pattern attribute specifies the regular expression used to validate the input value.
URL formats follow specific structures. For example, a URL may have a structure such as https://example.com/app1/index.html.
https://is called the "protocol" (also known as the "scheme" or "scheme identifier").example.comis called the "host" (or "domain")./app1/index.htmlis called the "file path".
hese structures can be specified using a regular expression. A regular expression is a pattern used to find specific combinations of characters in a string.
However, it is not possible to determine whether a domain actually exists using a regular expression alone.
The pattern attribute is designed to validate the format of the input value using a regular expression. However, whether a domain exists is a logical check that cannot be determined through simple format validation.
This validation can be performed using JavaScript or server-side scripts.
The pattern attribute can validate the format of an input value using a regular expression, as shown in the following example.
Checking URLs That Start with HTTP or HTTPS
Checks for URL formats that start with http:// or https://.
<input type="url" pattern="https?://.*" placeholder="https://example.com">
Checking for a Specific Domain (e.g., example.com)
Checks for URL formats that start with http:// or https:// and include the example.com domain.
<input type="url" pattern="https?://(www\.)?example\.com/.*" placeholder="https://example.com">
Checking Top-Level Domains (e.g., .com, .net)
Checks for URL formats that start with http:// or https:// and end with .com or .net.
<input type="url" pattern="https?://.*\.(com|net)" placeholder="https://example.com">
If the input matches the regular expression pattern specified by the pattern attribute, it passes validation; otherwise, it fails validation.
For a detailed guide to regular expressions, see MDN - Regular expressions.
required Attribute
The required attribute indicates that the control is required, meaning that the user must enter a value before submitting the form. It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<form>
<label for="user-url">Website URL (*required)</label>
<!-- Specify as a required input field -->
<input type="url" id="user-url" required>
<button type="submit">Submit</button>
</form>
You can see that the browser displays an error message for the required field.
value Attribute
The value attribute sets the initial value.
This value specifies the URL displayed in the element when the page loads.
<input type="url" value="https:///example.com">
disabled Attribute
The disabled attribute is used to disable the element. A disabled element does not function even if the user tries to enter or change its value, because its inherent functionality is disabled. In addition, its value is not submitted when the form is submitted.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
<input type="url" value="https:///example.com" disabled>
However, JavaScript, which can set the value attribute directly, can still change the value of the control.
readonly Attribute
The readonly attribute prevents the control from being changed or edited.
It is a Boolean attribute, meaning you only need to include the attribute name without specifying a value.
It is mainly used with the value attribute to display the value entered or stored in the control to the user while preventing it from being edited.
<input type="url" value="https:///example.com" readonly>
However, JavaScript, which can set the value attribute directly, can still change the value of the control.
maxlength Attribute
The maxlength attribute limits the maximum number of characters (maximum length) that the user can enter. When the maxlength attribute is specified for the element, it prevents the user from entering more characters than allowed.
- The attribute value must be a non-negative integer (0 or greater).
- If an invalid value is specified, the maximum length restriction is not applied.
- The maximum number of characters is calculated in standard character units (UTF-16 code units rather than bytes), so the same restriction applies regardless of the language, including English, numbers, and Korean. However, some emoji, such as 😀, are represented by two code units in UTF-16, so the maximum number of characters may not correspond to the number of characters perceived by the user.
Note
If a valid minlength attribute is specified for the element, the value of the maxlength attribute must be greater than or equal to that value.
<!-- Limit input to a maximum of 10 characters with maxlength="10" -->
<input type="url" maxlength="10" placeholder="10 characters max">
minlength Attribute
The minlength attribute specifies the minimum number of characters (minimum length) that the user must enter. When the minlength attribute is specified for the element, the user must enter at least the specified minimum number of characters.
Note
The minlength attribute only checks the minimum number of characters that must be entered. If the user does not enter anything in the element, an empty value is allowed. If you do not want to allow an empty value, use the required attribute as well.
- The attribute value must be a non-negative integer (0 or greater).
- If an invalid value is specified, the minimum length restriction is not applied.
- The minimum number of characters is calculated in standard character units (UTF-16 code units rather than bytes), so the same restriction applies regardless of the language, including English, numbers, and Korean. However, some emoji, such as 😀, are represented by two code units in UTF-16, so the minimum number of characters may not correspond to the number of characters perceived by the user.
Note
If a valid maxlength attribute is specified for the element, the value of the minlength attribute must be less than or equal to that value.
<form>
<label for="url">URL</label>
<!-- Require a minimum of 10 characters with minlength="10" -->
<input type="url" id="url" minlength="10" placeholder="Enter 10+ characters..." required>
<button type="submit">Submit</button>
</form>
- Check for Errors: Leave the input field empty or enter only 1 character, then click the [Submit] button. Confirm that the browser displays an error message indicating that the minimum input requirement (10 characters) has not been met.
- Check for Success: Enter 10 or more characters in the field, then click the [Submit] button again. Confirm that the form is submitted without an error.
name Attribute
The name attribute specifies the name of the element.
When the name attribute is specified, its value is used as the name of the data sent when the form is submitted. In JavaScript, the value of the name attribute is also used as the name of the element in the form.elements API.
size Attribute
The size attribute specifies the width of the element based on the number of characters.
The value specified by the size attribute specifies the visual width of the input field based on the number of characters. Therefore, the actual displayed width may vary depending on the font size (font-size) that is set.
The value of the size attribute must be specified as an integer greater than 0.
If the size attribute is not specified or an invalid value is set, most browsers use 20 as the default value.
The size attribute only sets the visual width of the input field and does not affect validation for limiting the number of characters entered when the form is submitted.
<p>
<label>
No size attribute
<input type="url">
</label>
</p>
<p>
<label>
Short input
<input type="url" size="5">
</label>
</p>
<p>
<label>
Long input
<input type="url" size="50">
</label>
</p>
If you specify a width for an element with the size attribute using CSS, the CSS width takes precedence.
list Attribute
The list attribute specifies the value of the id attribute of the <datalist> tag that provides a list of predefined options for the <input type="url">.
The user can select an option from this predefined list or enter a different value. When the user clicks or double-clicks the control, the corresponding suggested values are displayed in a drop-down menu. For example, in the Chrome browser, an arrow (▼) is also displayed on the right side of the control to indicate that predefined values are available.
<label for="user-url">URL</label>
<input type="url" list="urls" id="user-url">
<!-- Display in the drop-down menu -->
<datalist id="urls">
<option value="https://example.com"></option>
<option value="https://example.info"></option>
<option value="https://example.net"></option>
</datalist>
Accessibility Considerations
The placeholder attribute cannot replace the label of the control.
For more information, see Accessibility Considerations for the <input> Tag.
placeholder attribute cannot replace the label of the control.
<input type="url" placeholder="Website URL">
<label for="user-url">Website URL</label>
<input type="url" id="user-url" placeholder="https://example.com">
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
<input type="url"> |
1 | 12 | 1 | 1 |
placeholder
|
3 | 12 | 4 | 4 |
pattern
|
4 | 12 | 4 | 5 |
required
|
4 | 12 | 4 | 5 |
name
|
4 | 12 | 4 | 5 |
size
|
4 | 12 | 4 | 5 |
list
|
22 | 12 | 4 | 12.1 |
Specifications
| Specification | |
|---|---|
<input type="url">
|
HTML Standard #url-state-(type=url) |