How to restrict Special Characters in Javascript

checkSpcialChar function will restrict the special characters in the input box. We need to pass the event as a parameter for that function. We also change keycodes to allow or disallow more keys. <html> <head> <script type=”text/javascript”> function checkSpcialChar(event){ if(!((event.keyCode >= 65) && (event.keyCode <= 90) || (event.keyCode >= 97) && (event.keyCode <= 122) ||…


Function to restrict only numbers in textbox using javascript

 <script type="text/javascript">
 function validate(key)
     var keycode = (key.which) ? key.which : key.keyCode;
     var phn = document.getElementById('txtPhn');
     if (!(keycode==8 || keycode==46)&&(keycode < 48 || keycode > 57))
         return false;
         return true;
 <form id="form1">
         <input type="text" ID="txtPhn" onkeypress="return validate(event)"></input>