🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 表单验证 ```html <div class="container" id="dv"> <label for="qq">Q Q</label><input type="text" id="qq"><span></span><br /> <label>手机</label><input type="text" id="phone"><span></span><br /> <label>邮箱</label><input type="text" id="e-mail"><span></span><br /> <label>座机</label><input type="text" id="telephone"><span></span><br /> <label>姓名</label><input type="text" id="fullName"><span></span><br /> </div> <script src="public.js"></script> ``` ```javascript function getInput(input, reg) { // 输入框失焦事件 input.onblur = function () { if (reg.test(this.value)) { this.nextElementSibling.innerText = "正确了";//当前的下一个兄弟元素 this.nextElementSibling.style.color = "green"; } else { this.nextElementSibling.innerText = "错了"; this.nextElementSibling.style.color = "red"; } } } //正则要是严格模式的 getInput(my$("qq"), /^\d{5,11}$/); getInput(my$("phone"), /^\d{11}$/); getInput(my$("e-mail"), /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/); getInput(my$("telephone"),/^\d{3,4}[-]\d{8}$/); getInput(my$("fullName"),/^[\u4e00-\u9fa5]{2,6}$/); ```