OnClientClick验证服务端控件输入是否合法

<mce:script type="text/javascript"><!-- String.prototype.trim = function() { return this.replace(/(^/s*)|(/s*$)/g, ""); } String.prototype.ltrim = function() { return this.replace(/(^/s*)/g,""); } String.prototype.rtrim = function() { return this.replace(/(/s*$)/g,""); } //验证邮箱 function verifyEmail(txtEmail) { var regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((/.[a-zA-Z0-9_-]{2,3}){1,2})$/; return regex.test(txtEmail); } //验证电话号码(可验证27种格式) //详见:http://www.cnblogs.com/mephisto/archive/2009/01/15/841757.html function verifyPhone(txtPhone) { var regex = /(^(/d{2,4}[-_-—]?)?/d{3,8}([-_-—]?/d{3,8})?([-_-—]?/d{1,7})?$)|(^0?1[35]/d{9}$)/; return regex.test(txtPhone); } //验证控件输入是否合法 //纯粹整理所需,如此验证同一个控件是不可能成功的 :) function validate() { var txtValue=document.getElementById("txt").value; if(txtValue.trim()=="") { alert("不能为空!"); document.getElementById("txt").focus(); return false; } if(!verifyEmail(txtValue)) { alert("无效的Email!"); document.getElementById("txt").focus(); return false; } if(!verifyPhone(txtValue)) { alert("无效的电话号码!"); document.getElementById("txt").focus(); return false; } return true; } // --></mce:script>

<asp:Button ID="btn" Text="Submit" OnClientClick="return validate();" runat="server" />

你可能感兴趣的:(function,server,regex,email,button,电话)