在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>
相关推荐
会豪5 小时前
Electron-Vite (一)快速构建桌面应用
前端
中微子5 小时前
React 执行阶段与渲染机制详解(基于 React 18+ 官方文档)
前端
唐某人丶5 小时前
教你如何用 JS 实现 Agent 系统(2)—— 开发 ReAct 版本的“深度搜索”
前端·人工智能·aigc
中微子5 小时前
深入剖析 useState产生的 setState的完整执行流程
前端
遂心_5 小时前
JavaScript 函数参数传递机制:一道经典面试题解析
前端·javascript
小徐_23336 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
RoyLin6 小时前
TypeScript设计模式:适配器模式
前端·后端·node.js
遂心_6 小时前
深入理解 React Hook:useEffect 完全指南
前端·javascript·react.js
Moonbit6 小时前
MoonBit 正式加入 WebAssembly Component Model 官方文档 !
前端·后端·编程语言
龙在天6 小时前
ts中的函数重载
前端