Validation using Javascript
This function will prevent users from inputting incorrect data using Javascript. It is based on pressing the key. You will call the function with this method:
onkeypress="editKeyBoard(this,keybNumeric)"
For example:
<nested:text property="distributionWorkValue" size="5" onkeypress="editKeyBoard(this,keybNumeric)"/>
The keybNumeric is a string that is contained within the function. You can add different strings if you want to prevent users from inputting certain types of characters. This might not work if the user copies and pastes into the textarea.
<SCRIPT language=JavaScript>
//used for checking validation
function keybEdit(strValid, strMsg) {
/* Function: keybEdit
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.
*/
// 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;
}
void function editKeyBoard(objForm, objKeyb) {
/* Function: editKeyBoard
Creation Date: October 11, 2001
Programmer: Edmond Woychowsky
Purpose: The purpose of this function is to 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(objKeyb.getMessage());
window.event.returnValue = false; // Clear invalid character
objForm.focus(); // Set focus
}
}
var keybYN = new keybEdit('yn','Valid values are \'Y\' or \'N\'.');
var keybNumeric = new keybEdit('.0123456789','');
var keybNumericOnly = new keybEdit('0123456789','');
var keybNumericComma = new keybEdit(',0123456789','');
var keybAlpha = new keybEdit('abcdefghijklmnopqurstuvwxyz ','Alpha 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','Numeric input only!');
var keybAlphaNM = new keybEdit('abcdefghijklmnopqurstuvwxyz');
var keybAlphaNumericNM = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890.');
var keybDecimalNM = new keybEdit('01234567890.','Decimal input only!');
var keybDateNM = new keybEdit('01234567890/');
var key0_5 = new keybEdit('012345','Only input 0-5');
var keybNumericPhone = new keybEdit('-01234567890()','Numeric input only.');
var keyb = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890!.?,:;()@#$%&* ','No special characters!');
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890!@#$%^&*(){}][\;:/?><,.~- =\\/_ ','Please use only letters, numbers,special characters or spaces.');
var keybAlphaNumericS = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890!@#$%^&*(){}][\;:/?><,.~-_ =\\/?','Please use only letters or numbers or special characters.');
var keybEmail = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890@_.', 'Please use only letters, numbers, periods, @ or _.');
var keybAlphaNumericSNotAmbs = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890!@#$%^*(){}][\;:/?><,.~-_ =\\/?','Please do not input a space or &.');

1 comments:
There seems to be a bug in your javascript:
void function editKeyBoard(objForm, objKeyb) {
should be just:
function editKeyBoard(objForm, objKeyb) {
else it will not work.
Post a Comment