js逐步教实现表单系统(html逻辑 css逻辑 js逻辑)

简介: js逐步教实现表单系统(html逻辑 css逻辑 js逻辑)

html部分:

<div class="container">
    <form action="
    " class="form" id="form">
      <h1>注册</h1>
      <div class="form-control">
        <label for="username">用户名</label>
        <input type="text" id="username" name="username" placeholder="请输入用户名">
        <small>错误提示</small>
      </div>
      <div class="form-control">
        <label for="email">邮箱</label>
        <input type="text" id="email" name="email" placeholder="请输入邮箱">
        <small>错误提示</small>
      </div>
      <div class="form-control">
        <label for="password">密码</label>
        <input type="text" id="password" name="password" placeholder="请输入密码">
        <small>错误提示</small>
      </div>
      <div class="form-control">
        <label for="password2">确认密码</label>
        <input type="password" id="password2" name="password2" placeholder="请输入确认密码">
        <small>错误提示</small>
      </div>
      <button>提交</button>
    </form>
  </div>

图片展示:
在这里插入图片描述

注意一下:要写对哦.

css部分:

:root
    {
      --success-color:#2ecc71;
      --error-color:#e74c3c;
    }
    *{padding: 0px;margin: 0px;}
    body
    {
        background-color: #f9fafb;
        font-family: sans-serif;
        display: flex;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
    }
    .container
    {
      background-color: #fff;
      border-radius: 5px;
      box-shadow: 0 20px 10px rgba(0,0,0,0.3);
      width: 400px;
    }
    h1
    {
      text-align: center;
      margin: 0 0 20px;
    }
    .form
    {
      padding: 30px 40px;
    }
    .form-control
    {
     margin-bottom: 10px;
     padding-bottom: 20px;
     position: relative;
    }
   .form-control label {
      color: #777;
     display: block;
      margin-bottom: 5px;
    }
    .form-control input {
      width: 100%;
      border: 2px solid #f0f0f0;
      border-radius: 4px;
      display: block;
      font-size: 14px;
      padding: 10px;

    }
    .form-control input:focus {
      border-color: #777;
      outline: 0;
    }
    .form-control.success input {
      border-color: var(--success-color);
    }
    .form-control.error input {
  border-color: var(--error-color);
}
.form-control small {
   color: var(--error-color);
   position: absolute;
   bottom: 0;
   left: 0;
   visibility: hidden;
}
.form-control.error small {
  visibility: visible;
}
.form button {
 cursor: pointer;
  background-color: #3498db;
  border: 2px solid #3498db;
  border-radius: 4px;
  color: #fff;
  display: block;
  font-size: 16px;
  padding: 10px;
  margin-top: 20px;
  width: 100%; 
  }

图片展示:
在这里插入图片描述

注意一下:
css变量:格式:
他是一个选择器.
定义变量:

  --success-color:#2ecc71;

使用变量:

 border-color: var(--success-color);

js逻辑:

const form = document.getElementById("form");
    const username = document.getElementById("username");
    const email = document.getElementById("email");
    const password = document.getElementById("password");
    const password2 = document.getElementById("password2");
    //第一:先看看是否是按下了button,如果是就先去取消默认的事件,然后再
    form.addEventListener("submit",function(e)
    {
        e.preventDefault();
        //第二:再看看提交之前填写了吗?
        checkRequired([username,email,password,password2]);
        checkLength(username,3,15);
        checkLength(password,6,12);
        checkEmail(email);
        checkPasswordsMatch(password, password2);
    });
    function checkRequired(inputArr)
    {
        inputArr.forEach(function(input)//遍历这些表单看看哪一个没写
        {
            if(input.value.trim()==="")//去除了空格,如果没值的话就
            {
                showError(input,`${getKeyWords(input)}为必填项`);
            }
            else
            {
                showSuccess(input);
            }
        });
    }
    function getKeyWords(input)
    {
      return input.placeholder.slice(3);
    }
    function showError(input,message)
    {
        const formControl=input.parentElement;
        formControl.className="form-control error";
        const small=formControl.querySelector("small");
        small.innerText=message;
    }
    function showSuccess(input)
    {
        const formControl=input.parentElement;
        formControl.className="form-control success";
    }
    function checkLength(input,min,max)
    {
        if(input.value.length<min)
        {
            showError(input,`${getKeyWords(input)}至少${min}个字符`);
        }
        else if(input.value.length>max)
        {
            showError(input,`${getKeyWords(input)}少于${max}个字符`);
        }
        else
        {
          showSuccess(input);
        }
    }
    function  checkEmail(input)
    {
       const re = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;//匹配规则
       if(re.test(input.value.trim()))
       {
          showSuccess(input);
       }
       else
       {
           showError(input,"邮箱格式错误");
       }
    }
    function  checkPasswordsMatch(input1,input2)
    {
        if(input1.value!==input2.value)
        {
             showError(input2, "密码不匹配");
        }
    }

图片展示:
在这里插入图片描述

第一步:先获取东西.

const form = document.getElementById("form");
    const username = document.getElementById("username");
    const email = document.getElementById("email");
    const password = document.getElementById("password");
    const password2 = document.getElementById("password2");

第二步:
当提交的情况下要做什么功能:

form.addEventListener("submit",function(e)
    {
        e.preventDefault();
        checkRequired([username,email,password,password2]);
        checkLength(username,3,15);
        checkLength(password,6,12);
        checkEmail(email);
        checkPasswordsMatch(password, password2);
    });

注意一下;第一:取消系统默认的事件,
最重要的一点在于是否每一个都填了.
所以:

 function checkRequired(inputArr)
    {
        inputArr.forEach(function(input)
        {
            if(input.value.trim()==="")
            {
                showError(input,`${getKeyWords(input)}为必填项`);
            }
            else
            {
                showSuccess(input);
            }
        });
    }

注意一下;这个函数的功能是;看看是否每一个input都填啦。

function getKeyWords(input)
    {
      return input.placeholder.slice(3);
    }

这个函数的功能是去掉请输入三个字.

function showError(input,message)
    {
        const formControl=input.parentElement;
        formControl.className="form-control error";
        const small=formControl.querySelector("small");
        small.innerText=message;
    }

这个函数的功能是;错误啦就显示红色边框与字.

例如:
在这里插入图片描述

 function showSuccess(input)
    {
        const formControl=input.parentElement;
        formControl.className="form-control success";
    }

这个函数的功能是对啦就显示对的蓝色边框就行了.

例如:
在这里插入图片描述

function checkLength(input,min,max)
    {
        if(input.value.length<min)
        {
            showError(input,`${getKeyWords(input)}至少${min}个字符`);
        }
        else if(input.value.length>max)
        {
            showError(input,`${getKeyWords(input)}少于${max}个字符`);
        }
        else
        {
          showSuccess(input);
        }
    }

这个函数的功能是;用来看看用户名和密码缺少多少字符.或者说多了.按照下面的标准.
在这里插入图片描述

 function  checkEmail(input)
    {
       const re = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;//匹配规则
       if(re.test(input.value.trim()))
       {
          showSuccess(input);
       }
       else
       {
           showError(input,"邮箱格式错误");
       }
    }

这个函数的功能是看看邮箱的格式对或者不对,

function  checkPasswordsMatch(input1,input2)
    {
        if(input1.value!==input2.value)
        {
             showError(input2, "密码不匹配");
        }
    }

这个函数的功能是;看看两次输入的密码是否都一样。

相关文章
|
4天前
|
移动开发 HTML5
HTML5/CSS3粒子效果进度条代码
HTML5/CSS3进度条应用。这款进度条插件在播放进度过程中出现粒子效果,就像一些小颗粒从进度条上散落下来
15 0
HTML5/CSS3粒子效果进度条代码
|
5天前
|
前端开发 JavaScript 索引
CSS常见用法 以及JS基础语法
CSS常见用法 以及JS基础语法
12 0
|
7天前
|
JavaScript 前端开发
js和css以及js制作弹窗
js和css以及js制作弹窗
11 1
|
8天前
|
移动开发 前端开发 JavaScript
:掌握移动端开发:HTML5 与 CSS3 的高效实践
:掌握移动端开发:HTML5 与 CSS3 的高效实践
22 1
|
13天前
|
缓存 移动开发 前端开发
【专栏:HTML与CSS前端技术趋势篇】HTML与CSS在PWA(Progressive Web Apps)中的应用
【4月更文挑战第30天】PWA(Progressive Web Apps)结合现代Web技术,提供接近原生应用的体验。HTML在PWA中构建页面结构和内容,响应式设计、语义化标签、Manifest文件和离线页面的创建都离不开HTML。CSS则用于定制主题样式、实现动画效果、响应式布局和管理字体图标。两者协同工作,保证PWA在不同设备和网络环境下的快速、可靠和一致性体验。随着前端技术进步,HTML与CSS在PWA中的应用将更广泛。
|
13天前
|
前端开发 JavaScript 开发者
【专栏:HTML与CSS前端技术趋势篇】前端框架(React/Vue/Angular)与HTML/CSS的结合使用
【4月更文挑战第30天】前端框架React、Vue和Angular助力UI开发,通过组件化、状态管理和虚拟DOM提升效率。这些框架与HTML/CSS结合,使用模板语法、样式管理及组件化思想。未来趋势包括框架简化、Web组件标准采用和CSS在框架中角色的演变。开发者需紧跟技术发展,掌握新工具,提升开发效能。
|
13天前
|
前端开发 JavaScript UED
【专栏:HTML 与 CSS 前端技术趋势篇】Web 性能优化:CSS 与 HTML 的未来趋势
【4月更文挑战第30天】本文探讨了CSS和HTML在Web性能优化中的关键作用,包括样式表压缩、选择器优化、DOM操作减少等策略。随着未来趋势发展,CSS模块系统、自定义属性和响应式设计将得到强化,HTML新特性也将支持复杂组件构建。同时,应对浏览器兼容性、代码复杂度和性能功能平衡的挑战是优化过程中的重要任务。通过案例分析和持续创新,我们可以提升Web应用性能,创造更好的用户体验。
|
13天前
|
移动开发 前端开发 UED
【专栏:HTML与CSS前端技术趋势篇】渐进式增强与优雅降级在前端开发中的实践
【4月更文挑战第30天】前端开发中的渐进式增强和优雅降级是确保跨浏览器、跨设备良好用户体验的关键策略。渐进式增强是从基础功能开始,逐步增加高级特性,保证所有用户能访问基本内容;而优雅降级则是从完整版本出发,向下兼容,确保低版本浏览器仍能使用基本功能。实践中,遵循HTML5/CSS3规范,使用流式布局和响应式设计,检测浏览器特性,并提供备选方案,都是实现这两种策略的有效方法。选择合适策略优化网站,提升用户体验。
|
13天前
|
前端开发 开发者 UED
【专栏:HTML与CSS前端技术趋势篇】网页设计中的CSS Grid与Flexbox之争
【4月更文挑战第30天】本文对比了CSS Grid和Flexbox两种布局工具。Flexbox擅长一维布局,简单易用,适合导航栏和列表;CSS Grid则适用于二维布局,能创建复杂结构,适用于整个页面布局。两者各有优势,在响应式设计中都占有一席之地。随着Web标准发展,它们的结合使用将成为趋势,开发者需掌握两者以应对多样化需求。
|
13天前
|
前端开发 JavaScript 搜索推荐
【专栏:HTML 与 CSS 前端技术趋势篇】HTML 与 CSS 在 Web 组件化中的应用
【4月更文挑战第30天】本文探讨了HTML和CSS在Web组件化中的应用及其在前端趋势中的重要性。组件化提高了代码复用、维护性和扩展性。HTML提供组件结构,语义化标签增进可读性,支持用户交互;CSS实现样式封装、布局控制和主题定制。案例展示了导航栏、卡片和模态框组件的创建。响应式设计、动态样式、CSS预处理器和Web组件标准等趋势影响HTML/CSS在组件化中的应用。面对兼容性、代码复杂度和性能优化挑战,需采取相应策略。未来,持续发掘HTML和CSS潜力,推动组件化开发创新,提升Web应用体验。