Home > Web Front-end > JS Tutorial > How to use Layui to implement front-end form verification function

How to use Layui to implement front-end form verification function

PHPz
Release: 2023-10-27 19:44:02
Original
1547 people have browsed it

How to use Layui to implement front-end form verification function

How to use Layui to implement front-end form verification function

引言:
在前端开发中,表单的验证是必不可少的一项功能。通过对用户输入的数据进行验证,可以有效地提升用户体验和数据的准确性。Layui是一款轻量级的前端UI框架,提供了简洁易用的表单验证组件,大大减轻了开发人员的工作量。本文将介绍How to use Layui to implement front-end form verification function,并提供具体的代码示例。

一、Layui表单验证的基本使用

  1. 引入Layui:
    首先在页面中引入Layui框架的相关文件,包括layui.css和layui.js。
<link rel="stylesheet" href="path/to/layui.css">
<script src="path/to/layui.js"></script>
Copy after login
  1. 创建表单:
    在HTML中创建一个表单,并给需要验证的表单元素添加相应的class,如lay-verifylay-requried等。
<form class="layui-form" lay-filter="formDemo">
  <div class="layui-form-item">
    <label class="layui-form-label">用户名</label>
    <div class="layui-input-inline">
      <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">密码</label>
    <div class="layui-input-inline">
      <input type="password" name="password" required lay-verify="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="demo">立即提交</button>
      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
Copy after login
  1. 初始化验证:
    在页面加载完成后,使用Layui的form.render()方法对表单进行初始化,即进行表单的验证。
layui.use('form', function(){
  var form = layui.form;
  
  //自定义验证规则
  form.verify({
    username: function(value){
      if(value.length < 5){
        return '用户名长度不能少于5个字符';
      }
    },
    password: [/^[S]{6,12}$/,'密码必须6到12位,且不能出现空格']
  });
  
  //监听提交
  form.on('submit(demo)', function(data){
    layer.alert(JSON.stringify(data.field), {
      title: '最终的提交信息'
    })
    return false;
  });
});
Copy after login

二、常见表单验证示例

  1. 必填字段验证:
<input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
Copy after login
  1. 长度限制验证:
<input type="text" name="username" lay-verify="minLength|maxLength" placeholder="请输入用户名" autocomplete="off" class="layui-input">
Copy after login
  1. 自定义验证规则:
layui.use('form', function(){
  var form = layui.form;
  
  form.verify({
    username: function(value){
      if(value.length < 5){
        return '用户名长度不能少于5个字符';
      }
    }
  });
});
Copy after login
  1. 复杂密码验证:
layui.use('form', function(){
  var form = layui.form;
  
  form.verify({
    password: [/^[S]{6,12}$/,'密码必须6到12位,且不能出现空格']
  });
});
Copy after login

三、自定义验证提示样式
除了Layui提供的默认验证提示样式外,我们还可以自定义验证提示的样式。通过修改Layui的CSS文件中相关类名的样式,实现自定义的验证提示效果。

.layui-form-item .layui-input-inline .layui-form-mid{
  height: 24px;
  line-height: 24px;
  padding-left: 4px;
  color: #393D49;
}

.layui-form-item .layui-input-inline .layui-form-label::before{
  width: 0;
}
Copy after login

结论:
通过Layui提供的表单验证组件,我们可以快速实现前端表单验证的功能,并通过自定义验证规则和样式,满足不同项目的需求。Layui的表单验证组件简单易用,具有良好的扩展性和兼容性,是前端开发中不可或缺的一部分。

参考资料:

  1. Layui官网:https://www.layui.com/doc/modules/form.html
  2. Layui表单验证示例:https://www.layui.com/demo/form/verify.html

The above is the detailed content of How to use Layui to implement front-end form verification function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template