<asp:TextBox ID="CommentBox" runat="server" TextMode="multiLine" CssClass="commentTbx" onKeyDown="limitText(this,200);" onKeyUp="limitText(this,200);valid(this);"></asp:TextBox>
function valid(f) {
!(/^[A-zÑñ0-9-\s]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9-\s]/ig,''):null;
}
嗨上面是我的javascript函数,但我想只允许逗号,空格和fullstop!怎么可能?
答案 0 :(得分:1)
<script language="javascript" type="text/javascript">
function check(e) {
var keynum
var keychar
var numcheck
// For Internet Explorer
if (window.event)
{
keynum = e.keyCode
}
// For Netscape/Firefox/Opera
else if (e.which)
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
//List of special characters you want to restrict
if (keychar == "'" || keychar == "`")
{
return false;
}
else {
return true;
}
}
</script>
<asp:TextBox ID="txtName" runat="server" onkeypress="return check(event)" ></asp:TextBox>
希望这会有所帮助
答案 1 :(得分:1)
使用Regular Expression Validator。
使用ASP .NET验证控件的优点是它将在客户端和服务器端进行验证。
对于正则表达式,使用[\w\s\.\,]+
之类的东西应该有效。但您可能需要检查表达式。
答案 2 :(得分:0)
试试此代码
textBox = document.getElementById("textbox");
text = textBox.value;
text = text.replace(/[\\!"£$%^&\-\)\(*+_={};:'@#~¦\/<>\""\?|`¬\]\[]/g,'');
textBox.value = text;