首先在tp上有多种方法去判断留言是否为空,但是js是最方便也是最没有冲突的。
<form action="{:U('validate')}" method="post" name="myform"> <input type="hidden" name="lanmu" value="33"> <li> <div class="main_ly_t1"><em>留言标题:</em><input type="text" name="lc_title" id="lc_title" required> <span style="color:#C39766;">*</span></div> <div class="clear"></div> </li> <li> <div class="main_ly_t1"><em>姓名:</em><input type="text" name="lc_name" id="lc_name" required><span style="color:#C39766;">*</span></div> <div class="clear"></div> </li> <li> <div class="main_ly_t1"><em>E-MAIL:</em><input type="text" name="lc_email" id="lc_email" required><span style="color:#C39766;">*</span></div> <div class="clear"></div> </li> <li> <div class="main_ly_t1"><em>联系电话:</em><input type="text" name="lc_tel" id="lc_tel" required><span style="color:#C39766;">*</span></div> <div class="clear"></div> </li> <li> <div class="main_ly_t1"><em>留言内容:</em><textarea name="lc_content" id="lc_content" required></textarea></div> <div class="clear"></div> </li> <li> <div class="yao"> <div class="main_ly_t2"><input type="submit" id="tijiao" value="提 交" onClick="return doSubmit();" style="background:#000; border-radius:5px;border:0;"></div> <div class="main_ly_t3"><input type="reset" id="chongzhi" value="重 置" style="background:#000; border-radius:5px; border:0;"></div> </div> </li> <div class="clear"></div> </form>
现在有一个点击事件doSubmit(),我们做一个判断信息是否为空,给出提示,并赋予焦点。
<script type="text/javascript">function doSubmit(){ if (document.getElementById("lc_title").value==""){ alert("亲,请填写留言标题!"); document.getElementById("lc_title").focus(); //赋予焦点 return false; } if (document.getElementById("lc_name").value==""){ alert("亲,请填写您的姓名!"); document.getElementById("lc_name").focus(); return false; } if (document.getElementById("lc_email").value==""){ alert("亲,请填写电子邮件!"); document.getElementById("lc_email").focus(); return false; } if (!Isyx(document.getElementById("lc_email").value)){ alert("亲,请输入正确的电子邮件!"); document.getElementById("lc_email").focus(); return false; } if (document.getElementById("lc_tel").value==""){ alert("亲,请填写联系方式!"); document.getElementById("lc_tel").focus(); return false; } var reg =/(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/; //判断手机填写信息是否为正确手机号 if(!reg.test(document.getElementById("lc_tel").value)){ alert("亲,请输入正确的联系方式!"); document.getElementById("lc_tel").focus(); return false; } if (document.getElementById("lc_content").value==""){ alert("亲,请填写留言内容!"); document.etElementById("lc_content").focus(); return false; } return true;}</script>
这样就可以实现了
还没有人抢沙发呢~