做一个判断函数,用来检查是不是符合要求的看看MSDN上的类似代码吧:下面的代码示例使用 KeyPress 事件来禁止向控件输入字符。 // Boolean flag used to determine when a character other than a number is entered. private bool nonNumberEntered = false;// Handle the KeyDown event to determine the type of character entered into the control. private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // Initialize the flag to false. nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) { // Determine whether the keystroke is a number from the keypad. if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) { // Determine whether the keystroke is a backspace. if(e.KeyCode != Keys.Back) { // A non-numerical keystroke was pressed. // Set the flag to true and evaluate in KeyPress event. nonNumberEntered = true; } } } }// This event occurs after the KeyDown event and can be used to prevent // characters from entering the control. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { // Check for the flag being set in the KeyDown event. if (nonNumberEntered == true) { // Stop the character from being entered into the control since it is non-numerical. e.Handled = true; } }
判断
int txt;
try
{
txt = Convert.ToInt(TextBox.Text);
}
catch
{
//输入的不是数字
}if(txt < 0 || txt >255)
//只能输入0-255之间的数字
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Initialize the flag to false.
nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if(e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
}// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (nonNumberEntered == true)
{
// Stop the character from being entered into the control since it is non-numerical.
e.Handled = true;
}
}
建议LZ写个JS的判断,省的跟服务器交互
子网掩码:F1.F2.F3.F4。
先用“.”分隔成数组,判断数组长度是否是 4。
再判断每一个值是否是 0-255 之间。
再判断如果前一段不是 255,则后面的各断必为 0。
private void rGBRedTextBox_TextChanged(object sender, EventArgs e)
{
errorProvider.Clear(); if (rGBRedTextBox.Text != "")
RGBred = Convert.ToInt32(rGBRedTextBox.Text); if (RGBred > 255)
{
rGBRedTextBox.Text = "255";
errorProvider.SetError(rGBRedTextBox, "RGBRed value must between 0 and 255!");
} textBox1.BackColor = System.Drawing.Color.FromArgb(RGBred, RGBblue, RGBgreen); }
再限制只能输入数字,使用KEYPRESS事件 private void rGBRedTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == 8); }
把
textBox1.BackColor = System.Drawing.Color.FromArgb(RGBred, RGBblue, RGBgreen);
忽略,那个是在我的PORJECT里用的
2.用专用控件,第一种要求好像.NET本身就有提供
aspnetForm是form id,ctl00$ContentPlaceHolder1$description是文本框id<asp:TextBox ID="rebate_rate" onkeypress="check()" runat="server" CssClass="input_text"></asp:TextBox>
<asp:Button ID="BtnSubmit" runat="server" Text=" 提交 " CssClass="btn4_out" OnClientClick="return check_input111()" OnClick="BtnSubmit_Click" onmouseover="this.className='btn4_over'" onmouseout="this.className='btn4_out'" /><script language="javascript" type="text/jscript">
<!--
//只能输入数字
function check()
{
if(!(window.event.keyCode>=48 &&window.event.keyCode<=57)){window.event.keyCode=0;}
}
//不能大于255
function check_input111()
{
if (document.forms['aspnetForm'].ctl00$ContentPlaceHolder1$rebate_rate.value>255)
{
alert("优惠策略输入过长,最多不能超过1000个字符!");
return false;
}
}
-->
</script>