C# 限制文本框中的长度和输入掩码
我需要限制C#文本框中允许的位数 我还需要创建验证,以便它类似于手机号码,这意味着它必须以07开头,总共有11位数字C# 限制文本框中的长度和输入掩码,c#,winforms,validation,C#,Winforms,Validation,我需要限制C#文本框中允许的位数 我还需要创建验证,以便它类似于手机号码,这意味着它必须以07开头,总共有11位数字 有什么建议吗?您可以使用提供受控输入值。“07”后跟11位掩码将是\0\7000000000您可以使用提供受控输入值。“07”后跟11位掩码将是\0\7000000000您没有任何代码作为示例,因此,我将键入我的代码 要限制字符数,请键入以下代码: private bool Validation() { if (textBox.Text.Length != 11)
有什么建议吗?您可以使用提供受控输入值。“07”后跟11位掩码将是
\0\7000000000
您可以使用提供受控输入值。“07”后跟11位掩码将是\0\7000000000
您没有任何代码作为示例,因此,我将键入我的代码
要限制字符数,请键入以下代码:
private bool Validation()
{
if (textBox.Text.Length != 11)
{
MessageBox.Show("Text in textBox must have 11 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private bool Validation()
{
string s = textBox.Text;
string s1 = s.Substring(0, 1); // First number in brackets is from wich position you want to cut string, the second number is how many characters you want to cut
string s2 = s.Substring(1, 1);
if (s1 != "0" || s2 != "7")
{
MessageBox.Show("Number must begin with 07", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private void buttonAccept_Click(object sender, EventArgs e)
{
if (Validation() == false) return;
}
如果您希望文本框中的文本以“07”开头,则应键入以下代码:
private bool Validation()
{
if (textBox.Text.Length != 11)
{
MessageBox.Show("Text in textBox must have 11 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private bool Validation()
{
string s = textBox.Text;
string s1 = s.Substring(0, 1); // First number in brackets is from wich position you want to cut string, the second number is how many characters you want to cut
string s2 = s.Substring(1, 1);
if (s1 != "0" || s2 != "7")
{
MessageBox.Show("Number must begin with 07", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private void buttonAccept_Click(object sender, EventArgs e)
{
if (Validation() == false) return;
}
您可以将其合并到一个资源方法中,并且可以随时调用它。如果您想调用某些方法(例如,当您单击“接受”按钮时),只需键入以下代码:
private bool Validation()
{
if (textBox.Text.Length != 11)
{
MessageBox.Show("Text in textBox must have 11 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private bool Validation()
{
string s = textBox.Text;
string s1 = s.Substring(0, 1); // First number in brackets is from wich position you want to cut string, the second number is how many characters you want to cut
string s2 = s.Substring(1, 1);
if (s1 != "0" || s2 != "7")
{
MessageBox.Show("Number must begin with 07", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private void buttonAccept_Click(object sender, EventArgs e)
{
if (Validation() == false) return;
}
您没有任何代码作为示例,因此,我会键入我的代码 要限制字符数,请键入以下代码:
private bool Validation()
{
if (textBox.Text.Length != 11)
{
MessageBox.Show("Text in textBox must have 11 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private bool Validation()
{
string s = textBox.Text;
string s1 = s.Substring(0, 1); // First number in brackets is from wich position you want to cut string, the second number is how many characters you want to cut
string s2 = s.Substring(1, 1);
if (s1 != "0" || s2 != "7")
{
MessageBox.Show("Number must begin with 07", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private void buttonAccept_Click(object sender, EventArgs e)
{
if (Validation() == false) return;
}
如果您希望文本框中的文本以“07”开头,则应键入以下代码:
private bool Validation()
{
if (textBox.Text.Length != 11)
{
MessageBox.Show("Text in textBox must have 11 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private bool Validation()
{
string s = textBox.Text;
string s1 = s.Substring(0, 1); // First number in brackets is from wich position you want to cut string, the second number is how many characters you want to cut
string s2 = s.Substring(1, 1);
if (s1 != "0" || s2 != "7")
{
MessageBox.Show("Number must begin with 07", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private void buttonAccept_Click(object sender, EventArgs e)
{
if (Validation() == false) return;
}
您可以将其合并到一个资源方法中,并且可以随时调用它。如果您想调用某些方法(例如,当您单击“接受”按钮时),只需键入以下代码:
private bool Validation()
{
if (textBox.Text.Length != 11)
{
MessageBox.Show("Text in textBox must have 11 characters", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private bool Validation()
{
string s = textBox.Text;
string s1 = s.Substring(0, 1); // First number in brackets is from wich position you want to cut string, the second number is how many characters you want to cut
string s2 = s.Substring(1, 1);
if (s1 != "0" || s2 != "7")
{
MessageBox.Show("Number must begin with 07", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox.Focus();
return false;
}
return true;
}
private void buttonAccept_Click(object sender, EventArgs e)
{
if (Validation() == false) return;
}
您使用的是WPF、winforms还是html?winforms、visual studio 2012 for c#尽我所能,应该只是验证字符的限制,前两个字符的验证必须以“”开头,但我不确定该怎么做?您使用的是WPF、winforms还是html?winforms、visual studio 2012 for c#尽我所能,应该是字符限制的验证,前两个字符的验证必须以“”开头,但我不确定如何进行。第二个
验证方法中的条件是错误的。更不用说创建两个字符串实例(每个实例包含一个字符)更难读取,并且会在堆上创建不必要的对象。我还想知道使用名为Validation
的方法返回一个显示mesage框的bool是否是一个好主意。感觉不是很容易重用…我看到我不小心键入了&&而不是| |,现在它可以工作了。我正在使用这样的验证,它正在工作。。。如果您有其他代码与我们共享,我将很乐意尝试。:)第二个验证方法中的条件是错误的。更不用说创建两个字符串实例(每个实例包含一个字符)更难读取,并且会在堆上创建不必要的对象。我还想知道使用名为Validation
的方法返回一个显示mesage框的bool是否是一个好主意。感觉不是很容易重用…我看到我不小心键入了&&而不是| |,现在它可以工作了。我正在使用这样的验证,它正在工作。。。如果您有其他代码与我们共享,我很乐意尝试。:)