Jump to content

Restricting User Input


Guest FirefoxRocks
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...