if (/[\u007f-\uffff]/.test("是否是汉字") && ((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) {}
// 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; } }摘自MSDN,只允许输入数字
^[\u4e00-\u9fa5\d]+$
function onlyEng()
{
if(!(event.keyCode>=65&&event.keyCode<=90))
event.returnValue=false;
}
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//考虑小键盘上的数字键
event.returnValue=false;
}
function isEmail(strEmail) {
if (strEmail.search(/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/) != -1)
return true;
else
alert("oh");
}
</script>
<div align="center">
<center>
<table border="1" width="90%" cellspacing="0" cellpadding="0" style="font-size:12px" bordercolor="#000000" bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr>
<td width="100%">只能是汉字:<input onkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')"></td>
</tr>
<tr>
<td width="100%">只能是英语字符:<input onkeydown="onlyEng();"></td>
</tr>
<tr>
<td width="100%">只能是数字:<input onkeydown="onlyNum();"></td>
</tr>
<tr>
<td width="100%">只能是英文字母和数字:<input onkeyup="value=value.replace(/[/W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))"></td>
</tr>
<tr>
<td width="100%">验证email格式:<input type=text onblur=isEmail(this.value)></td>
</tr>
<tr>
<td width="100%">不能输入汉字<INPUT onpaste="return false;" style="IME-MODE: disabled; WIDTH: 400px" value="I only accept English input! Thanks for corporation!" name=textfield> </td>
</tr>
{}
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;
}
}摘自MSDN,只允许输入数字
.charOnly
{
ime-mode: disabled;
}
</style>
//或者:
<script language="javascript" type="text/javascript">
function checkNum() {
if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 43 && event.keyCode != 45)
{
event.keyCode = 0;
event.returnValue = false;
}
}
</script><asp:TextBox ID="txtMobile" runat="server" Width="260px" MaxLength="20"
CssClass="charOnly" onkeypress="checkNum()" ></asp:TextBox>//或者:
AutoCompleteType="Disabled" MaxLength="4" onkeypress="checkNum()"