一些正则在js使用方法
输入框直接正则判断
<input type="password" name="pwd" placeholder="密码只能以数字\英文\@\." onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'')" />
在js中判断
<div class="control-group"> <label class="laber_from">手机号</label> <div class="controls" > <INPUT id='tel_id' class="input_from" type="text" placeholder=" 请输入手机号" name='tel'/> <P id="p2" class=help-block></P> </div> </div> <script> $('#tel_id').blur(function(){ var tel_val = $(this).val(); if(tel_val =='' || !/((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)/.test(tel_val)) { $('#p2').html('<p>手机格式有误</p>').css('color','red'); }else{ $('#p2').empty(); } }) </script>
满足了条件才可以按提交按钮
<FORM action="__URL__/question" method="post"> <DIV class="control-group"> <LABEL class="laber_from">旧密保问题</LABEL> <DIV class="controls" ><INPUT class="input_from" type="text" placeholder=" 请输入密保问题" onkeyup="value=value.replace(/^[a-zA-Z0-9]\w{16,17}$/g,'')" name='question'/><P id="p0" class=help-block></P></DIV> </DIV> <DIV class="control-group"> <LABEL class="laber_from">新保问题</LABEL> <DIV class="controls" ><INPUT class="input_from" type="text" placeholder=" 请输入密保问题" onkeyup="value=value.replace(/^[a-zA-Z0-9]\w{16,17}$/g,'')" name='question'/><P id="p1" class=help-block></P></DIV> </DIV> <DIV class="control-group"> <LABEL class="laber_from" ></LABEL> <DIV class="controls" ><button disabled class="btn btn-success" style="width:120px;" >提交</button></DIV> </DIV> </FORM> <script> $('input:eq(1)').blur(function(){ var one = $(this).val(); $('#p1').empty(); var errorMsg = '密保问题不能为空'; if(one =='') { $('#p1').append('<span style="color:red">'+errorMsg+'</span>'); $('button').attr('disabled','disabled'); }else if(one != '' && input_0 !=''){ $('button').attr('disabled',false); } }) </script>