首頁  >  文章  >  web前端  >  如何在HTML中新增一個用於檢查輸入元素值的正規表示式?

如何在HTML中新增一個用於檢查輸入元素值的正規表示式?

WBOY
WBOY轉載
2023-08-19 19:33:12677瀏覽

如何在HTML中新增一個用於檢查輸入元素值的正規表示式?

The task we are going to perform in this article is about how to add a regular expression that an input elements value is checked against in HTML.

將用於檢查輸入元素值的正規表示式由HTML的模式屬性指定。以下輸入類型與此屬性相容:文字、密碼、日期、搜尋、電子郵件等。

Syntax

以下是HTML 標籤的pattern屬性的語法。

<input pattern = "regular_exp">

For getting more idea on how to add a regular expression that an input elements value is checked against in HTML, let’s look into the following examples

對於type="password"

The type="password" attribute on the tag creates a text field where users can safely enter a password. Depending on the browser, characters are replaced as they are entered by a dotor ("•") asterisk ("*").

Example

在下面的範例中,我們使用了元素,其類型為」password」。

<!DOCTYPE html>
<html>
<body>
   <form action="#">
      <label for="tutorial">Password:</label>
      <input type="password" id="tutorial" name="tutorial" pattern=".{8,}"
      title="Eight or More Characters">
      <input type="submit">
   </form>
</body>
</html>

在執行上述腳本時,它將產生一個類型為密碼的輸入字段,並設定一個模式以確保它滿足限制條件才能提交,否則將顯示一個警示框。

For input field restricted with three letters

在這種情況下,我們限制了輸入字段,只允許使用者輸入三個字母。

Example

Considering the following we are using the input field restricted with three letters with no numbers or special characters.

<!DOCTYPE html>
<html>
<body>
   <form action="#">
      <label for="tutorial">Country Code:</label>
      <input type="text" id="tutorial" name="tutorial"
      pattern="[A-Za-z]{3}" title=" country code"><br><br>
      <input type="submit">
   </form>
</body>
</html>

當腳本被執行時,它將顯示一個帶有名稱「countrycode」的輸出,以及一個僅限於三個字母的提交按鈕。如果不符合限制,它將顯示一個警告框。

以上是如何在HTML中新增一個用於檢查輸入元素值的正規表示式?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除