Restrict some special characters from the textbox

Posted by Blessybaby under JavaScript category on | Views : 33634
function RestrictChar()
get the keycode when the user pressed any key in application
var exp = String.fromCharCode(window.event.keyCode)
//Below line will have the special characters that is not allowed you can add if you want more for some characters you need to add escape sequence
var r = new RegExp("[-_,/'().0-9a-zA-Z \r]", "g");
if (exp.match(r) == null)
window.event.keyCode = 0
return false;

in code behind we need to add below code for calling this function
txtuser.Attributes.Add("onkeypress", "javascript:return RestrictChar()");

Comments or Responses

Login to post response