我有一个文本框,我只想在其中输入字母数字字符和标点符号。 jsFiddle在这里,并且javascript实现如下所示:

var TheCleanString = TheInput.replace(/(^\s+|[^a-zA-Z0-9 \\s\(\)\.\-]+)/g, '');


如您所见,这仅允许使用字母数字字符,删除前导空格,并允许使用括号,点和连字符。

现在,我想使用SAME正则表达式来验证来自客户端的字符串是否已通过此正则表达式。我有这样的事情:

public bool MadeIt(TheCandidateString)
{
   if (TheCandidateString passed this
       regex /(^\s+|[^a-zA-Z0-9 \\s\(\)\.\-]+)/g
       then return true)
}


最好的方法是什么?

谢谢。

最佳答案

public bool MadeIt(string TheCandidateString)
{
    string regex= @"yourRegex";
    var match = Regex.Match(TheCandidateString, regex, RegexOptions.IgnoreCase);
    return match.Success;
}

关于c# - 从客户端正则表达式实现服务器端正则表达式验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21331466/

10-14 22:53
查看更多