Introduction to layui form validation

Release: 2020-06-12 17:22:37
forward
3085 people have browsed it

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>
Copy after login

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;;
             }
         }
     });

     });
Copy after login

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>
Copy after login
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);		      
    		 }	    
    		 });

});
Copy after login

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!

Related labels:
source:cnblogs.com
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