Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在标签中显示文本框中输入的第一个特殊字符_C#_Asp.net_Regex - Fatal编程技术网

C# 如何在标签中显示文本框中输入的第一个特殊字符

C# 如何在标签中显示文本框中输入的第一个特殊字符,c#,asp.net,regex,C#,Asp.net,Regex,我创建了一个regex函数,并在保存数据时调用它 public static bool CheckSpecialCharacter(string value) { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"[~`!@#$%^*()=|\{}';.,<>]"); if (regex.IsMatch(value)) { ret

我创建了一个regex函数,并在保存数据时调用它

public static bool CheckSpecialCharacter(string value)
{
   System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"[~`!@#$%^*()=|\{}';.,<>]");
   if (regex.IsMatch(value))
   {
      return false;
   }
   else
   {
      return true;
   }
}
现在,我想附加在文本框中输入的第一个特殊字符,而不是写“不允许使用特殊字符”,因此 如果输入了@,则消息应理解为“不允许使用特殊字符@”

有可能这样做吗?请帮助。谢谢。

尝试以下代码

public static string CheckSpecialCharacter(string value)
{
   System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"[~`!@#$%^*()=|\{}';.,<>]");
   var match = regex.Match(value);
   if (match.Success)
   {
      return match.Value;
   }
   else
   {
      return string.empty;
   }
}
或者您可以通过返回bool并在函数中添加一个
out参数来实现,但我不建议这样做

编辑-在Javascript中执行相同的操作

function CheckSpecialCharacter(value)
{
  var res = value.match(/[~`!@#$%^*()=|\{}';.,<>]/g);
  return res == null ? "" : res[0];
}
试试这个:

public static bool CheckSpecialCharacter(string value, out string character)
{
    var regex = new System.Text.RegularExpressions.Regex(@"[~`!@#$%^*()=|\{}';.,<>]");
    var match = regex.Match(value);
    character = regex.Match(value).Value;
    return match.Length == 0;
}

您只需使用正则表达式中的
Matches(string)
函数获取匹配项,然后检查第一个元素,如下所示:

var regex = new Regex(@"[~`!@#$%^*()=|\{}';.,<>]");
var matches = regex.Matches("This contains # two b@d characters");
if (matches.Count > 0)
{
    var firstBadCharacter = matches[0];
}

没有必要使用regex两次。只需使用
regex.Match
,然后检查
Match.Success
是否正确。@KD这很有效,非常感谢!你能告诉我如何在javascript中做同样的事情吗?我编辑了答案,请检查代码是否与javascript配合良好
var value = CheckSpecialCharacter(document.getElementById("txt_ExpName1").value);

if(value != "")
{
   document.getElementById("lblErrMsg").innerHTML = value + " Special characters not allowed";
}
public static bool CheckSpecialCharacter(string value, out string character)
{
    var regex = new System.Text.RegularExpressions.Regex(@"[~`!@#$%^*()=|\{}';.,<>]");
    var match = regex.Match(value);
    character = regex.Match(value).Value;
    return match.Length == 0;
}
string character;
if (ClassName.CheckSpecialCharacter(txt_ExpName1.Text, out character) == false)
{
    lblErrMsg.Text = character + " Special characters not allowed";
    return;
}
var regex = new Regex(@"[~`!@#$%^*()=|\{}';.,<>]");
var matches = regex.Matches("This contains # two b@d characters");
if (matches.Count > 0)
{
    var firstBadCharacter = matches[0];
}
throw new ArgumentException("Special character '" + firstBadCharacter + "' not allowed.");