Topic: window.event & window.event.which (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=29080" title="Pages that link to Topic: window.event &amp;amp; window.event.which (Page 1 of 1)" rel="nofollow" >Topic: window.event &amp; window.event.which <span class="small">(Page 1 of 1)</span>\

 
Boudga
Maniac (V) Mad Scientist

From: Jacks raging bile duct....
Insane since: Mar 2000

posted posted 04-02-2007 16:09

I found some scripting here for restricting keyboard input for form data. The only issue I've encountered so far is that it was written for IE and doesn't work right in Firefox as Firefox doesn't support window.event. The question I have is how do I modify the script at the window.event area to accomodate Firefox? Thanks in advance for any and all help given.

code:
var keybYN = new keybEdit('yn','Valid values are \'Y\' or \'N\'.');
var keybNumeric = new keybEdit('01234567890','Numeric input only.');
var keybAlpha = new keybEdit('abcdefghijklmnopqurstuvwxy ','Alpha input only.');
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxy01234567890 ','Alpha-numeric input only.');
var keybDecimal = new keybEdit('01234567890.','Decimal input only.');
var keybDate =  new keybEdit('01234567890/','Date input only');;
var keybYNNM = new keybEdit('yn');
var keybNumericNM = new keybEdit('01234567890');
var keybAlphaNM = new keybEdit('abcdefghijklmnopqurstuvwxy');
var keybAlphaNumericNM = new keybEdit('abcdefghijklmnopqurstuvwxy01234567890');
var keybDecimalNM = new keybEdit('01234567890.');
var keybDateNM = new keybEdit('01234567890/');;

function keybEdit(strValid, strMsg) {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to be a constructor for
						the keybEdit object.  keybEdit objects are used by the
						function editKeyBoard to determine which keystrokes are
						valid for form objects.  In addition, if an error occurs,
						they provide the error message.
						
						Please note that the strValid is converted to both
						upper and lower case by this constructor.  Also, that
						the error message is prefixed with 'Error:'.
						
						The properties for this object are the following:
							valid	=	Valid input characters
							message	=	Error message
							
						The methods for this object are the following:
							getValid()	=	Returns a string containing valid
											characters.
							getMessage()=	Returns a string containing the
											error message.

		Update Date:	Programmer:			Description:
	*/

	//	Variables
	var reWork = new RegExp('[a-z]','gi');		//	Regular expression\

	//	Properties
	if(reWork.test(strValid))
		this.valid = strValid.toLowerCase() + strValid.toUpperCase();
	else
		this.valid = strValid;

	if((strMsg == null) || (typeof(strMsg) == 'undefined'))
		this.message = '';
	else
		this.message = strMsg;

	//	Methods
	this.getValid = keybEditGetValid;
	this.getMessage = keybEditGetMessage;
	
	function keybEditGetValid() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getValid method
						for the keybEdit object.  Please note that most of the
						following logic is for handling numeric keypad input.

		Update Date:		Programmer:			Description:
	*/

		return this.valid.toString();
	}
	
	function keybEditGetMessage() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getMessage method
						for the keybEdit object.

		Update Date:	Programmer:			Description:
	*/
	
		return this.message;
	}
}




function editKeyBoard(objForm, objKeyb) {
	/*	Function:		editKeyBoard
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to edit edit keyboard input
						to determine if the keystrokes are valid.
	
		Update Date:		Programmer:			Description:
	*/

	strWork = objKeyb.getValid();
	strMsg = '';							// Error message
	blnValidChar = false;					// Valid character flag
	
	
	
	// Part 1: Validate input
	if(!blnValidChar)
		for(i=0;i < strWork.length;i++)
			if(window.event.keyCode == strWork.charCodeAt(i)) {
				blnValidChar = true;

				break;
			}

	// Part 2: Build error message
	if(!blnValidChar) {
		if(objKeyb.getMessage().toString().length != 0)
			alert('Error: ' + objKeyb.getMessage());

		window.event.returnValue = false;		// Clear invalid character
		objForm.focus();						// Set focus
	}
}

function setEvents() {
	
	//document.all.txtYN.onkeypress = new Function('editKeyBoard(this,keybYN)');
	//document.all.txtNumeric.onkeypress = new Function('editKeyBoard(this,keybNumeric)');
	//document.all.txtAlpha.onkeypress = new Function('editKeyBoard(this,keybAlpha)');
	document.getElementById('name_first').onkeypress = new Function('editKeyBoard(this,keybAlpha)');
	//document.all.txtAlphaNumeric.onkeypress = new Function('editKeyBoard(this,keybAlphaNumeric)');
	//document.all.txtDecimal.onkeypress = new Function('editKeyBoard(this,keybDecimal)');
	//document.all.txtDate.onkeypress = new Function('editKeyBoard(this,keybDate)');
}



(Edited by Boudga on 04-02-2007 16:11)

Edmond Woychowsky
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Apr 2007

posted posted 04-11-2007 19:42

I made some changes, try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="JavaScript">
<!-- <![CDATA[
var keybYN = new keybEdit('yn','Valid values are \'Y\' or \'N\'.');
var keybNumeric = new keybEdit('01234567890','Numeric input only.');
var keybAlpha = new keybEdit('abcdefghijklmnopqurstuvwxyz','Alpha input only.');
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890/ -,.=!@#$%^&*()_+\'":;','Alpha-numeric input only.');
var keybDecimal = new keybEdit('01234567890.','Decimal input only.');
var keybDate = new keybEdit('01234567890/','Date input only');;
var keybYNNM = new keybEdit('yn','');
var keybNumericNM = new keybEdit('01234567890','');
var keybAlphaNM = new keybEdit('abcdefghijklmnopqurstuvwxyz','');
var keybAlphaNumericNM = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890/ -,.=!@#$%^&*()_+\'":;','');
var keybDecimalNM = new keybEdit('01234567890.','');
var keybDateNM = new keybEdit('01234567890/','');;

function keybEdit(strValid, strMsg) {
/*
Function: keybEdit
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function is to be a constructor for
the keybEdit object. keybEdit objects are used by the
function editKeyBoard to determine which keystrokes are
valid for form objects. In addition, if an error occurs,
they provide the error message.

Please note that the strValid is converted to both
upper and lower case by this constructor. Also, that
the error message is prefixed with 'Error:'.

The properties for this object are the following:
valid = Valid input characters
message = Error message

The methods for this object are the following:
getValid() = Returns a string containing valid characters.
getMessage() = Returns a string containing the error message.

Update Date: Programmer: Description:
*/

// Variables
var reWork = new RegExp('[a-z]','gi'); // Regular expression\

// Properties
if(reWork.test(strValid))
this.valid = strValid.toLowerCase() + strValid.toUpperCase();
else
this.valid = strValid;

if((strMsg == null) || (typeof(strMsg) == 'undefined'))
this.message = '';
else
this.message = strMsg;

// Methods
this.getValid = keybEditGetValid;
this.getMessage = keybEditGetMessage;

function keybEditGetValid() {
/* Function: keybEdit
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function act as the getValid method
for the keybEdit object. Please note that most of the
following logic is for handling numeric keypad input.

Update Date: Programmer: Description:
*/

return this.valid.toString();
}

function keybEditGetMessage() {
/* Function: keybEdit
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function act as the getMessage method
for the keybEdit object.

Update Date: Programmer: Description:
*/

return this.message;
}
}

function editKeyBoard(evt, objKeyb) {
/* Function: editKeyBoard
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function is to edit edit keyboard input
to determine if the keystrokes are valid.

Update Date: Programmer: Description:
February 26, 2002 Edmond Woychowsky Added logic to handle control key
sequences.
May 10, 2002 Edmond Woychowsky Modified logic for message-less
errors.
June 5, 2002 Edmond Woychowsky Modified logic to prevent dollar signs
in numeric fields.
June 11, 2003 Edmond Woychowsky Modified logic to accomadate backspace.
April 11, 2007 Edmond Woychowsky Updated for cross-browser DOM support,
also remove keypad logic.
*/

var strWork = objKeyb.getValid();
var strMsg = ''; // Error message
var blnValidChar = false; // Valid character flag
var intCode = evt.charCode == undefined ? evt.keyCode : evt.charCode;

// Part 1: Validate input
if(!blnValidChar)

switch(true) {
case(intCode == 8):
case(intCode == 9):
case(intCode == 10):
case(intCode == 13):
blnValidChar = true;

break;
default:
for(i=0;i < strWork.length;i++)
if(intCode == strWork.charCodeAt(i)) {
blnValidChar = true;

break;
}

break;
}

// Part 2: Build error message
if(!blnValidChar) {
if(objKeyb.getMessage().toString().length != 0)
alert('Error: ' + objKeyb.getMessage());

evt.stopPropagation = true;

try {
evt.preventDefault();
}
catch(e) {
window.event.returnValue = false; // Clear invalid character
}
}
}
// ]]> -->
</script>
</head>
<body>
<input id="xyzzy" onkeypress="editKeyBoard(event,keybNumeric)"/>
<br/>
<input id="plugh" onkeypress="editKeyBoard(event,keybAlpha)"/>
</body>
</html>

Bono
Obsessive-Compulsive (I) Inmate

From: Sunderland
Insane since: May 2007

posted posted 05-31-2007 12:47

Hi Edmond,
First off, great script!

had to alter a line in the script to get it working properly in Firefox2, to get the intCode I altered the following line:

code:
var intCode = evt.charCode == undefined ? evt.keyCode : evt.charCode;



to be:

code:
var intCode = (evt.charCode) ? evt.charCode : evt.keyCode;



this allowed the switch statement to process the backspace, delete key etc. properly. I also added a couple of switches to the switch in order to cater for users navigating back and fourth the input control, i know that the folks I work for would need that lol, users! The additions were:

code:
case(intCode == 37):
case(intCode == 39):



thought you might like to know in case you would like to update your script...

Regards,

Bono



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu