正则表达式--表单验证
2017-07-07 18:32
204 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <style type="text/css"> body{background: #ccc;} label{width: 40px; display: inline-block;} span{color: red;} .container{margin: 100px auto; width: 400px; padding: 50px; line-height: 40px; border: 1px solid #999; background: #efefef;} span{margin-left: 30px; font-size: 12px;} </style> <body> <div class="container"> <label>QQ</label><input type="text" id="inp1"><span></span><br> <label>手机</label><input type="text" id="inp2"><span></span><br> <label>昵称</label><input type="text" id="inp3"><span></span><br> <label>密码</label><input type="password" id="inp4"><span></span><br> <label>邮箱</label><input type="text" id="inp5"><span></span><br> </div> </body> <script type="text/javascript"> var g = function(id){return document.getElementById(id);} var changed = function(id, fn){ g(id).onchange = fn; } var setNextHtml = function(id, html){ g(id).nextSibling.innerHTML = html; } var val = function(id){ return g(id).value; } changed('inp1', function(){ if(/^\d{5,11}$/.test(val('inp1'))){ setNextHtml('inp1', ''); }else{ setNextHtml('inp1', '请输入5到11位数字'); } }); changed('inp2', function(){ if(/^1\d{12}$/.test(val('inp2'))){ setNextHtml('inp2', ''); }else{ setNextHtml('inp2', '数字1开头,13位的数字'); } }); changed('inp3', function(){ if(/^[\w\-\u4e00-\u9fa5]{1,7}$/.test(val('inp3'))){ setNextHtml('inp3', '') }else{ setNextHtml('inp3', '1到7位') } }); changed('inp4', function(){ if(/^[A-Z]\w{1,15}$/.test(val('inp4'))){ setNextHtml('inp4', ''); }else{ setNextHtml('inp4', '首字母大写,1到16位'); } }); changed('inp5', function(){ if(/^[\w\-\.]+\@[\w]+\.[\w]{2,4}/.test(val('inp5'))){ setNextHtml('inp5', '') }else{ setNextHtml('inp5', '请输入正确邮箱格式') } }); </script> </html>
相关文章推荐
- JS通用表单验证函数,基于javascript正则表达式http://www.ccvita.com/60.html
- javascript 正则表达式+表单验证 结合运用简单例子
- js验证 正则表达式 包含手机号码、座机号码、电子邮箱、密码、姓名、身份证号码等表单数据的验证
- 正则表达式基本语法及表单验证操作详解【基于JS】
- JavaScript正则表达式表单验证
- js用正则表达式来验证表单
- 表单验证--js正则表达式
- 常用验证表单正则表达式
- 2016/2/25 1、<表单验证<form></form> 2、正则表达式 3、事件
- 一些表单验证常用的正则表达式收集
- js部分---表单验证;(含正则表达式)
- js正则表达式案例之表单验证
- html5中如何使用正则表达式进行表单验证
- 表单验证 正则表达式实例 Email验证 电话号码验证 中文汉字验证
- 简单的表单验证正则表达式
- 表单验证的一些正则表达式
- 常用的表单验证正则表达式
- php下常用表单验证的正则表达式
- 正则表达式的应用——简单的表单验证
- 前端表单验证常用的15个JS正则表达式