[一篮饭特稀原创,转载请注明出处http://www.cnblogs.com/wanghafan/archive/2012/05/10/2493864.html]
js脚本正则判空和数字验证
View Code
1 <script type="text/javascript"> 2 function CheckNullAndType() 3 { 4 var ErrorStr=''; 5 if($('#tb_FContractName').val().replace(/(^\s*)|(\s*$)/g, "")=='') ErrorStr+=$('#lb_FContractName').text()+"不能为空;\r\n"; 6 if($('#hid_ConstructionUnitID').val().replace(/(^\s*)|(\s*$)/g, "")=='') ErrorStr+=$('#lb_ConstructionUnitID').text()+"不能为空;\r\n"; 7 if($('#hid_Professional').val().replace(/(^\s*)|(\s*$)/g, "")=='') ErrorStr+=$('#lb_Professional').text()+"不能为空;\r\n"; 8 if(ErrorStr=='') 9 { 10 var reg = new RegExp("^[0-9]*$"); 11 if(!reg.test($('#tb_FContractCount').val())) ErrorStr+=$('#lb_FContractCount').text()+"必须为整数;\r\n"; 12 if(!reg.test($('#tb_BackCount').val())) ErrorStr+=$('#lb_BackCount').text()+"必须为整数;\r\n"; 13 reg = new RegExp("^([+-]?)\\d*\\.?\\d+$"); 14 if(!reg.test($('#tb_FeeStandards').val())) ErrorStr+=$('#lb_FeeStandards').text()+"必须为数字;\r\n"; 15 if(!reg.test($('#tb_TotalFee').val())) ErrorStr+=$('#lb_TotalFee').text()+"必须为数字;\r\n"; 16 if(!reg.test($('#tb_FContractMonney').val())) ErrorStr+=$('#lb_FContractMonney').text()+"必须为数字;\r\n"; 17 if(ErrorStr=='') 18 { 19 return true; 20 } 21 else 22 { 23 alert(ErrorStr); 24 return false; 25 } 26 } 27 else 28 { 29 alert(ErrorStr); 30 return false; 31 } 32 } 33 </script>
html按钮
View Code
1 <form id="form1" runat="server"> 2 <asp:Button CssClass="btn_Common" runat="server" Text="保存" ID="btn_OK" OnClick="btn_OK_Click"/> 3 </form>
后台代码
View Code
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!IsPostBack) 4 { 5 btn_OK.Attributes.Add("onClick", "return CheckNullAndType()"); 6 } 7 }