<input id="txtInput" type="text" />
<input id="Button1" type="button" value="button" οnclick="javascript:inputWord()" />
js
<script language="javascript" type="text/javascript">
function inputWord() {
var vRtxtUserName = document.getElementById("txtInput").value;
var reg = /\s{2,}/g;
vRtxtUserName = vRtxtUserName.replace(reg, " ");//将多空格过滤
if (!/^[a-zA-Z0-9_\u4e00-\u9fa5\" "]+$/.test(vRtxtUserName)) {
alert('输入框只能包括中文、英文字母、数字和下划线或者空格!');
window.location.href = 'index.html';
}
window.location.href = '/xxxx.aspx?input='+encodeURIComponent(vRtxtUserName);
}
</script>
cs
protected void Page_Load(object sender, EventArgs e)
{
bool bolInput = IsValidChineseEnglishSpace(HttpUtility.UrlDecode(Request.QueryString["input"]));
}
public bool IsValidChineseEnglishSpace(string input)
{
return Regex.IsMatch(input, @"^[a-zA-Z0-9_\u4e00-\u9fa5\' ']+$");
}
//去除的办法
/// <summary>
/// 替换字符中文、字母、数字以外的字符替换成空。
/// </summary>
/// <param name="asString"></param>
/// <returns></returns>
public static string TrimCharacter(string asString)
{
string sResult = "";
string sCharacterRegx = "[^a-zA-Z0-9\u4E00-\u9FFF]";
Regex rgVal = new Regex(sCharacterRegx);
sResult = rgVal.Replace(asString, "");
return sResult;
}