收藏
回答

weui.js表单验证组件正则验证失败怎么解决?

<!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

最后一次编辑于  2019-11-05
回答关注问题邀请回答
收藏

1 个回答

  • Hhdber
    Hhdber
    2020-05-25

    请教下你是如何解决的

    2020-05-25
    有用
    回复
登录 后发表内容
问题标签