weui.js表单验证组件正则验证失败怎么解决?
发布于 6 年前 作者 xiongli 9797 次浏览 来自 官方Issues
<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!--  head处引入css(css必须引入,可以使用cdn。因为weui.js里面生成的页面元素会用到) -->
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.0.1/weui.min.css">
    <script type="text/javascript" src="https://res.wx.qq.com/open/libs/weuijs/1.2.1/weui.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
 
<body>
    <form id="myform" style="margin-top: 100px;">
        <input type="tel" placeholder="输入你现在的手机号" emptyTips="请输入手机号" notMatchTips="请输入正确的手机号" required pattern="TEL">
        <input type="button" id="btn" value="验证">
    </form>
    <script type="text/javascript">
        //点击按钮时验证
        $(document).on('click', '#btn', function () {
            weui.form.validate('#myform', function (error) {
                console.log(error)
                if (!error) {
                    var loading = weui.loading('提交中...');
                    setTimeout(function () {
                        loading.hide();
                        weui.toast('提交成功', 1000);
                    }, 1500);
                }
                // return true; // 当return true时,不会显示错误
            }, {
                //定义正则表达式
                regexp: {
                    TEL: /0/
                }
            });
        })
    </script>
    </script>
</body>
 
</html>

试了很多次,只能过空验证,过不了匹配验证。

https://github.com/Tencent/weui.js/blob/master/docs/component/form.md

补充:代码高亮样式太难看了,上在线预览版本吧。

http://js.jirengu.com/jetuxaruta/1/edit?html,output

回到顶部