JavaScript:
<script language="javascript" type="text/javascript"> function textbox(obj, maxvalue, minvalue) { if (obj.value != "") { if (parseInt(obj.value) == obj.value && parseInt(obj.value) >= minvalue && parseInt(obj.value) <= maxvalue) { //do nothing } else { alert('Please enter ' + minvalue + ' - ' + maxvalue + ''); obj.value = ""; } } } </script>
HTML:
<body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" TextMode="Number"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server" TextMode="Number"></asp:TextBox> </div> </form> </body>
C#:
protected void Page_Load(object sender, EventArgs e) { //onblur TextBox1.Attributes.Add("onblur", "textbox(" + TextBox1.ClientID + ", 10, 0)"); TextBox2.Attributes.Add("onblur", "textbox(" + TextBox1.ClientID + ", 5, 1)"); //onkeydown //TextBox1.Attributes.Add("onkeydown", "textbox(" + TextBox1.ClientID + ", 10, 0)"); //TextBox2.Attributes.Add("onkeydown", "textbox(" + TextBox1.ClientID + ", 5, 1)"); }
沒有留言:
張貼留言