在css中如何实现表单验证效果

表单控件一些属性:

:valid —— 用于匹配输入值为合法的元素

:invalid —— 用于匹配输入值为非法的元素

pattern —— 属性规定用于验证输入字段的正则表达式

required —— 属性规定必需在提交之前填写输入字段

注意:required 属性适用于这些 <input> 类型:text, search, url, telephone, email, password, date pickers, number, checkbox, radio,textarea 以及 file。

:valid/:invalid —— 选择器用于在表单元素中的值是合法/非法时设置指定样式。

注意: :valid/:invalid 选择器只作用于能指定区间值的元素,例如 input 元素中的 min 和 max 属性,以及正确的 email 字段, 合法的数字字段等。
一、示例

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>表单</title>    
  <link rel="stylesheet" href="css/regisit.css">
  <style>
    *{
      margin: 0px;
      padding: 0px;
      box-sizing: border-box;
    }
    ol {
      width: 400px;
      margin: 50px;
    }
    li {
      clear: both;
      list-style-type: none;
      margin: 0 0 10px;
    }
    li:nth-last-child(1) {
      text-align: center;
    }
    label {
      display: block;
      float: left;
      margin: 0 10px 0 0;
      padding: 5px;
      text-align: right;
      width: 100px;
    }
    input{
     padding: 0 10px;
     outline: none;
     border: 1px solid #ccc;
     width: 260px;
     height: 30px;
     transition: all 300ms;
   }
   input:focus {
    outline: none;
   }
   /*input内容合法,边框颜色*/
   input:valid {
     border-color: green;
     box-shadow: inset 5px 0 0 green;
   }
   /*input内容合法且鼠标未移开,边框颜色*/
   input:focus:valid {
     border-color: yellow;
     box-shadow: inset 5px 0 0 yellow;
   }
   /*input内容非法,边框颜色*/
   input:invalid:required {
     border-color: red;
     box-shadow: inset 5px 0 0 red;
   }
   input[type="submit"] {
    border: none;
    box-shadow: 0 0 5px #7ab526;
    cursor: pointer;
    padding: 7px;
    width: 150px;
    background: linear-gradient(90deg, #5ada40 0%, #eb59c6 100%);
  }
  input[type="reset"] {
    margin-left: 10px;
    border: none;
    box-shadow: 0 0 5px #7ab526;
    cursor: pointer;
    padding: 7px;
    width: 150px;
    background: linear-gradient(90deg, #4FE7CE 0%, #E65032 100%);
  }
</style>
</head>
<body>
  <form>
   <ol>
     <li>
       <label for="url">姓名:</label>
       <input type="text" required name="" id="name">
     </li>
     <li>
       <label for="tel">手机号:</label>
       <input type="text" placeholder="请输入手机号" maxlength="11" pattern="^1[3456789]\d{9}$" required/>
     </li>
     <li>
       <input type="submit" name="" value="提交表单">
       <input type="reset" name="" value="清空表单">
     </li>
   </ol>
 </form>
</body>
</html>

相关推荐

  1. css如何实现验证效果

    2023-12-27 12:48:01       64 阅读
  2. Spring Boot使用Validation进行验证

    2023-12-27 12:48:01       43 阅读
  3. 耀斑层-如何Unity实现耀斑亮光效果

    2023-12-27 12:48:01       114 阅读
  4. uniapp验证

    2023-12-27 12:48:01       74 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 12:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 12:48:01       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 12:48:01       82 阅读
  4. Python语言-面向对象

    2023-12-27 12:48:01       91 阅读

热门阅读

  1. 如何强制 App 在 iOS 后台不断开与融云的长连接?

    2023-12-27 12:48:01       85 阅读
  2. 活动运营常用的ChatGPT通用提示词模板

    2023-12-27 12:48:01       58 阅读
  3. modbus-tcp-rtu协议图表

    2023-12-27 12:48:01       43 阅读
  4. leetcode | go | 第600题 | 不含连续1的非负整数

    2023-12-27 12:48:01       65 阅读
  5. vue中的动态组件和混入

    2023-12-27 12:48:01       51 阅读
  6. 2023年腾讯云活动:2核2G4M轻量服务器3年540元

    2023-12-27 12:48:01       63 阅读
  7. 分布式信号量(Redis)

    2023-12-27 12:48:01       56 阅读
  8. Spring缓存注解@Cacheable、@CachePut、@CacheEvict

    2023-12-27 12:48:01       59 阅读