如何在文本框中只允许特定的字母表?
How to allow only specific alphabets in a Textbox?
我能否让我的文本框只允许 L & P 作为用户输入的前两个字母.. 例如:LP12345678901。我希望这个例子使问题具体...
我试过了
if(myTextBox.Text.StartsWith("LP"))
我也试过正则表达式...但我们不能用字母表来具体说明对吗?
这不是技术解决方案,而是用户体验建议:
如果我总是必须输入 "LP" 作为第一个字母并且不允许我输入不同的字母或数字,那么不要让我每次都输入它们。您已经确定 "LP" 是 的前两个字母。我再打一遍也没意义。我无法从中得到任何好处。即使我完美进入,我能达到的最好结果是"no error message"。
如果您的前两个字母必须是 "LP",那么将 "LP" 作为标签放在文本框之前,让用户只输入数字。当您处理用户输入的内容时,请在其前面加上 "LP"。
试试这个代码:
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (myTextBox.Text.Length == 2)
{
if (myTextBox.Text.StartsWith("LP"))
{
//yourcode
}
else
{
myTextBox.Text = string.Empty;
}
}
}
public static bool ValidateSerNo(string ser)
{
if (!string.IsNullOrEmpty(ser))
{
if (ser.Trim().Length == 11)
{
if (ser.Trim().ToUpper().Substring(0, 2) == "LP")
{
if (Microsoft.VisualBasic.Information.IsNumeric(ser.Substring(2, 9)))
{
return true;
}
}
}
}
return false;
}
private void btn_Save_Click(object sender, EventArgs e)
{
if ((BattVoltMmnt.ValidateSerNo(txtbox_Serialno.Text.Trim().ToUpper()) == false))
{ MessageBox.Show("Enter correct serial number");
}
}
这对你有帮助。
function Valid() {
var re = "^[LP][0-9]{11}$";
var str = document.getElementById("txtTSection").value;
var myArray = str.match(re);
if (myArray == null) {
alert("Incorrect Format");
$("#txtTSection").css("border-color", "red");
return false;
}
else {
$('#txtTSection').css('border-color', '');
}
}
<asp:TextBox ID="txtTSection" runat="server" CssClass="form-control" TabIndex="1" MaxLength="13" onfocusout="Valid()" ></asp:TextBox>
我能否让我的文本框只允许 L & P 作为用户输入的前两个字母.. 例如:LP12345678901。我希望这个例子使问题具体...
我试过了
if(myTextBox.Text.StartsWith("LP"))
我也试过正则表达式...但我们不能用字母表来具体说明对吗?
这不是技术解决方案,而是用户体验建议:
如果我总是必须输入 "LP" 作为第一个字母并且不允许我输入不同的字母或数字,那么不要让我每次都输入它们。您已经确定 "LP" 是 的前两个字母。我再打一遍也没意义。我无法从中得到任何好处。即使我完美进入,我能达到的最好结果是"no error message"。
如果您的前两个字母必须是 "LP",那么将 "LP" 作为标签放在文本框之前,让用户只输入数字。当您处理用户输入的内容时,请在其前面加上 "LP"。
试试这个代码:
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (myTextBox.Text.Length == 2)
{
if (myTextBox.Text.StartsWith("LP"))
{
//yourcode
}
else
{
myTextBox.Text = string.Empty;
}
}
}
public static bool ValidateSerNo(string ser)
{
if (!string.IsNullOrEmpty(ser))
{
if (ser.Trim().Length == 11)
{
if (ser.Trim().ToUpper().Substring(0, 2) == "LP")
{
if (Microsoft.VisualBasic.Information.IsNumeric(ser.Substring(2, 9)))
{
return true;
}
}
}
}
return false;
}
private void btn_Save_Click(object sender, EventArgs e)
{
if ((BattVoltMmnt.ValidateSerNo(txtbox_Serialno.Text.Trim().ToUpper()) == false))
{ MessageBox.Show("Enter correct serial number");
}
}
这对你有帮助。
function Valid() {
var re = "^[LP][0-9]{11}$";
var str = document.getElementById("txtTSection").value;
var myArray = str.match(re);
if (myArray == null) {
alert("Incorrect Format");
$("#txtTSection").css("border-color", "red");
return false;
}
else {
$('#txtTSection').css('border-color', '');
}
}
<asp:TextBox ID="txtTSection" runat="server" CssClass="form-control" TabIndex="1" MaxLength="13" onfocusout="Valid()" ></asp:TextBox>