Jump to content

Restricting User Input


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

I would like to know how to create a function that will restrict text input. It will launch onkeyup.I need to restrict the input to alphanumeric characters and the asterisk (*) symbol. The function will simply not allow invalid characters to be entered.I know how to check for characters but how do I prevent them from being inputted?

Link to comment
Share on other sites

You'd call the function on the keypress event

function onlyLetters(e) {  // Get the event object (window.event is for IE)  e = e?e:window.event;  // Cross-browser find the code of the key that was pressed  key = e.keyCode?e.keyCode:(e.which?e.which:'');  // If the character of the given keycode is not a letter or * then don't write it in the input  if(!/[a-z\*]/i.test(String.fromCharCode(key))) {	(e.preventDefault)?e.preventDefault:'';	return false;  }}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...