Reputation:
<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;
}
Hi above is my javascript function, but i want to allow commas, space and fullstop ONLY! How is it possible?
Upvotes: 0
Views: 5936
Reputation: 2795
Try this code
textBox = document.getElementById("textbox");
text = textBox.value;
text = text.replace(/[\\!"£$%^&\-\)\(*+_={};:'@#~¦\/<>\""\?|`¬\]\[]/g,'');
textBox.value = text;
Upvotes: 0
Reputation: 19862
Use a Regular Expression Validator.
The advantage for using the ASP .NET validation control is that it will be validated both client side and server side.
As for the regular expression, using something like [\w\s\.\,]+
should work. But you might need to check the expression.
Upvotes: 1
Reputation: 2215
<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>
hope this helps
Upvotes: 1