search
HomeWeb Front-endLayui TutorialIntroduction to layui form validation

Introduction to layui form validation

Jun 12, 2020 pm 05:22 PM
layui

Introduction to layui form validation

1. Sorting verification

html code

<div class="layui-form-item">
             <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
              <input type="number" name="sort" autocomplete="off" min="0" max="99999"  lay-verify="sort" class="layui-input">
            </div> 
          </div>

type="number" After that, the effect as shown above will appear behind the text box, and Only numbers and decimal points can be entered

js code

layui.use([ "form", "layer" ], function() {
var    form = layui.form;
var    layer = layui.layer;
    //表单验证
    form.render();//这句一定要加,占坑
     form.verify({

         sort:[/^$|^[0-9]{0,5}$/, &#39;只能是数字且范围0~99999!&#39;]//这个就是上面的排序lay-verify="sort" 
         ,remark: function (value){
             if(value.length > 200){
                 return &#39;长度大于200!请重新输入&#39;;
             }
         }
     });

     });

2. Amount verification

The effect is the same as sorting

<div class="layui-form-item">
            <label class="layui-form-label">余额(元)</label>
            <div class="layui-input-block">
                <input type="number" name="balance" id="balance" lay-verify="money"
                 autocomplete="off" placeholder="单位:元"  
                    class="layui-input" >
            </div>
        </div>
layui.use([ "form", "layer", "laydate" ], function() {
    laydate = layui.laydate;
    form = layui.form;
    layer = layui.layer;// 表单验证
    form.render();
    form.verify({
        lenth50 : function(value) {
            if (value.length > 50) {
                return &#39;长度大于50!请重新输入&#39;;
            }
        },
        lenth400 : function(value) {
            if (value.length > 400) {
                return &#39;长度大于400!请重新输入&#39;;
            }
        },
        money : function(value) {
            if (value.length > 0) {
                var reg = /(^[0-9]([0-9]+)?(\.[0-9]+)$)|(^(0){1}$)|(^([0-9]+)?$)/;
                if (!reg.test(value)) {
                    return &#39;输入格式有误&#39;;
                }
            }
            if (value.length > 50) {
                return &#39;长度大于50!请重新输入&#39;;
            }
        }

    });    
    //下面这段代码主要是用于强制清除当用户输入0开头的数字,比如0000,02323,匹配到的话就为0
    //如果要在layui里面 写js事件之类的代码就一定要放在use组件模块里面,这是其中一个坑!
    $(&#39;#balance&#39;).bind(&#39;input propertychange&#39;, function() {
    		 var balance = $("#balance").val();		      
    		 var zero = /^(0){2,}$|^(0)([0-9])?$/;		      
    		 if (zero.test(balance)) {			      
    		 $(&#39;#balance&#39;).val(0);		      
    		 }	    
    		 });

});

MorelayuiKnowledge please pay attention to PHP Chinese websitelayui tutorialcolumn

The above is the detailed content of Introduction to layui form validation. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!