: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
/********************************************************************
* Limit the characters that may be entered in a text field
* Common options: alphanumeric, alphabetic or numeric
* http://github.com/KevinSheedy/jquery.alphanum
*********************************************************************/
// API ///////////////////////////////////////////////////////////////////
$.fn.alphanum = function( settings ) {
var combinedSettings = getCombinedSettingsAlphaNum( settings );
setupEventHandlers( $collection, trimAlphaNum, combinedSettings );
$.fn.alpha = function( settings ) {
var defaultAlphaSettings = getCombinedSettingsAlphaNum( 'alpha' );
var combinedSettings = getCombinedSettingsAlphaNum( settings, defaultAlphaSettings );
setupEventHandlers( $collection, trimAlphaNum, combinedSettings );
$.fn.numeric = function( settings ) {
var combinedSettings = getCombinedSettingsNum( settings );
setupEventHandlers( $collection, trimNum, combinedSettings );
numericField_Blur( this, settings );
// End of API /////////////////////////////////////////////////////////////
// Start Settings ////////////////////////////////////////////////////////
var DEFAULT_SETTINGS_ALPHANUM = {
allow: '', // Allow extra characters
disallow: '', // Disallow extra characters
allowSpace: true, // Allow the space character
allowNumeric: true, // Allow digits 0-9
allowUpper: true, // Allow upper case characters
allowLower: true, // Allow lower case characters
allowCaseless: true, // Allow characters that don't have both upper & lower variants - eg Arabic or Chinese
allowLatin: true, // a-z A-Z
allowOtherCharSets: true, // eg �, �, Arabic, Chinese etc
maxLength: NaN // eg Max Length
var DEFAULT_SETTINGS_NUM = {
allowPlus: false, // Allow the + sign
allowMinus: true, // Allow the - sign
allowThouSep: true, // Allow the thousands separator, default is the comma eg 12,000
allowDecSep: true, // Allow the decimal separator, default is the fullstop eg 3.141
allowLeadingSpaces: false, maxDigits: NaN, // The max number of digits
maxDecimalPlaces: NaN, // The max number of decimal places
maxPreDecimalPlaces: NaN, // The max number digits before the decimal point
max: NaN, // The max numeric value allowed
min: NaN // The min numeric value allowed
// Some pre-defined groups of settings for convenience
var CONVENIENCE_SETTINGS_ALPHANUM = {
allowNumeric: false, allowUpper: true, allowLower: false, allowCaseless: true
allowNumeric: false, allowUpper: false, allowLower: true, allowCaseless: true
// Some pre-defined groups of settings for convenience
var CONVENIENCE_SETTINGS_NUMERIC = {
allowPlus: false, allowMinus: true, allowThouSep: false, allowDecSep: false
allowPlus: false, allowMinus: false, allowThouSep: false, allowDecSep: false
var BLACKLIST = getBlacklistAscii() + getBlacklistNonAscii();
var DIGITS = getDigitsMap();
var LATIN_CHARS = getLatinCharsSet();
// Return the blacklisted special chars that are encodable using 7-bit ascii
function getBlacklistAscii() {
var blacklist = '!@#$%^&*()+=[]\\\';,/{}|":<>?~`.-_';
blacklist += ' '; // 'Space' is on the blacklist but can be enabled using the 'allowSpace' config entry
// Return the blacklisted special chars that are NOT encodable using 7-bit ascii
// We want this .js file to be encoded using 7-bit ascii so it can reach the widest possible audience
// Higher order chars must be escaped eg "\xAC"
// Not too worried about comments containing higher order characters for now (let's wait and see if it becomes a problem)
function getBlacklistNonAscii() {
var blacklist = '\xAC' // �
// End Settings ////////////////////////////////////////////////////////
// Implementation details go here ////////////////////////////////////////////////////////
function setupEventHandlers( $textboxes, trimFunction, settings ) {
$textboxes.each( function() {
var $textbox = $( this );
$textbox.on( 'keyup change paste', function( e ) {
if ( e.originalEvent && e.originalEvent.clipboardData && e.originalEvent.clipboardData.getData ) pastedText = e.originalEvent.clipboardData.getData(
// setTimeout is necessary for handling the 'paste' event
trimTextbox( $textbox, trimFunction, settings, pastedText );
$textbox.on( 'keypress', function( e ) {
// Determine which key is pressed.
// If it's a control key, then allow the event's default action to occur eg backspace, tab
var charCode = !e.charCode ? e.which : e.charCode;
if ( isControlKey( charCode ) || e.ctrlKey || e.metaKey ) // cmd on MacOS
var newChar = String.fromCharCode( charCode );
// Determine if some text was selected / highlighted when the key was pressed
var selectionObject = $textbox.selection();
var start = selectionObject.start;
var end = selectionObject.end;
var textBeforeKeypress = $textbox.val();
// The new char may be inserted:
// 4) User highlights some text and then presses a key which would replace the highlighted text
// Here we build the string that would result after the keypress.
// If the resulting string is invalid, we cancel the event.
// Unfortunately, it isn't enough to just check if the new char is valid because some chars
// are position sensitive eg the decimal point '.'' or the minus sign '-'' are only valid in certain positions.
var potentialTextAfterKeypress = textBeforeKeypress.substring( 0, start ) + newChar + textBeforeKeypress.substring( end );
var validatedText = trimFunction( potentialTextAfterKeypress, settings );
// If the keypress would cause the textbox to contain invalid characters, then cancel the keypress event
if ( validatedText != potentialTextAfterKeypress ) e.preventDefault();
// Ensure the text is a valid number when focus leaves the textbox
// This catches the case where a user enters '-' or '.' without entering any digits
function numericField_Blur( inputBox, settings ) {
var fieldValueNumeric = parseFloat( $( inputBox ).val() );
var $inputBox = $( inputBox );
if ( isNaN( fieldValueNumeric ) ) {
if ( isNumeric( settings.min ) && fieldValueNumeric < settings.min ) $inputBox.val( '' );
if ( isNumeric( settings.max ) && fieldValueNumeric > settings.max ) $inputBox.val( '' );
function isNumeric( value ) {
function isControlKey( charCode ) {
if ( charCode >= 32 ) return false;
if ( charCode == 10 ) return false;
if ( charCode == 13 ) return false;
// One way to prevent a character being entered is to cancel the keypress event.
// However, this gets messy when you have to deal with things like copy paste which isn't a keypress.
// Which event gets fired first, keypress or keyup? What about IE6 etc etc?
// Instead, it's easier to allow the 'bad' character to be entered and then to delete it immediately after.
function trimTextbox( $textBox, trimFunction, settings, pastedText ) {
var inputString = $textBox.val();
if ( inputString == '' && pastedText.length > 0 ) inputString = pastedText;
var outputString = trimFunction( inputString, settings );
if ( inputString == outputString ) return;
var caretPos = $textBox.alphanum_caret();
$textBox.val( outputString );
//Reset the caret position
if ( inputString.length == (outputString.length + 1) ) $textBox.alphanum_caret( caretPos - 1 ); else $textBox.alphanum_caret( caretPos );
function getCombinedSettingsAlphaNum( settings, defaultSettings ) {
if ( typeof defaultSettings == 'undefined' ) defaultSettings = DEFAULT_SETTINGS_ALPHANUM;
var userSettings, combinedSettings = {};
if ( typeof settings === 'string' ) userSettings = CONVENIENCE_SETTINGS_ALPHANUM[settings]; else if ( typeof settings == 'undefined' ) userSettings = {}; else userSettings = settings;
$.extend( combinedSettings, defaultSettings, userSettings );
if ( typeof combinedSettings.blacklist == 'undefined' ) combinedSettings.blacklistSet = getBlacklistSet(
combinedSettings.allow, combinedSettings.disallow );
function getCombinedSettingsNum( settings ) {
var userSettings, combinedSettings = {};
if ( typeof settings === 'string' ) userSettings = CONVENIENCE_SETTINGS_NUMERIC[settings]; else if ( typeof settings == 'undefined' ) userSettings = {}; else userSettings = settings;
$.extend( combinedSettings, DEFAULT_SETTINGS_NUM, userSettings );
// This is the heart of the algorithm
function alphanum_allowChar( validatedStringFragment, Char, settings ) {
if ( settings.maxLength && validatedStringFragment.length >= settings.maxLength ) return false;
if ( settings.allow.indexOf( Char ) >= 0 ) return true;
if ( settings.allowSpace && (Char == ' ') ) return true;
if ( settings.blacklistSet.contains( Char ) ) return false;
if ( !settings.allowNumeric && DIGITS[Char] ) return false;
if ( !settings.allowUpper && isUpper( Char ) ) return false;
if ( !settings.allowLower && isLower( Char ) ) return false;
if ( !settings.allowCaseless && isCaseless( Char ) ) return false;
if ( !settings.allowLatin && LATIN_CHARS.contains( Char ) ) return false;
if ( !settings.allowOtherCharSets ) {
if ( DIGITS[Char] || LATIN_CHARS.contains( Char ) ) return true; else return false;
function numeric_allowChar( validatedStringFragment, Char, settings ) {
if ( isMaxDigitsReached( validatedStringFragment, settings ) ) return false;
if ( isMaxPreDecimalsReached( validatedStringFragment, settings ) ) return false;
if ( isMaxDecimalsReached( validatedStringFragment, settings ) ) return false;
if ( isGreaterThanMax( validatedStringFragment + Char, settings ) ) return false;
if ( isLessThanMin( validatedStringFragment + Char, settings ) ) return false;
if ( settings.allowPlus && Char == '+' && validatedStringFragment == '' ) return true;
if ( settings.allowMinus && Char == '-' && validatedStringFragment == '' ) return true;
if ( Char == THOU_SEP && settings.allowThouSep && allowThouSep( validatedStringFragment, Char ) ) return true;
// Only one decimal separator allowed
if ( validatedStringFragment.indexOf( DEC_SEP ) >= 0 ) return false;
if ( settings.allowDecSep ) return true;
function countDigits( string ) {
// Error handling, nulls etc
return string.replace( /[^0-9]/g, '' ).length;
function isMaxDigitsReached( string, settings ) {
var maxDigits = settings.maxDigits;
if ( maxDigits == '' || isNaN( maxDigits ) ) return false; // In this case, there is no maximum
var numDigits = countDigits( string );
if ( numDigits >= maxDigits ) return true;
function isMaxDecimalsReached( string, settings ) {
var maxDecimalPlaces = settings.maxDecimalPlaces;
if ( maxDecimalPlaces == '' || isNaN( maxDecimalPlaces ) ) return false; // In this case, there is no maximum
var indexOfDecimalPoint = string.indexOf( DEC_SEP );
if ( indexOfDecimalPoint == -1 ) return false;
var decimalSubstring = string.substring( indexOfDecimalPoint );
var numDecimals = countDigits( decimalSubstring );
if ( numDecimals >= maxDecimalPlaces ) return true;
function isMaxPreDecimalsReached( string, settings ) {
var maxPreDecimalPlaces = settings.maxPreDecimalPlaces;
if ( maxPreDecimalPlaces == '' || isNaN( maxPreDecimalPlaces ) ) return false; // In this case, there is no maximum
var indexOfDecimalPoint = string.indexOf( DEC_SEP );
if ( indexOfDecimalPoint >= 0 ) return false;
var numPreDecimalDigits = countDigits( string );
if ( numPreDecimalDigits >= maxPreDecimalPlaces ) return true;
function isGreaterThanMax( numericString, settings ) {
if ( !settings.max || settings.max < 0 ) return false;
var outputNumber = parseFloat( numericString );
if ( outputNumber > settings.max ) return true;
function isLessThanMin( numericString, settings ) {
if ( !settings.min || settings.min > 0 ) return false;
var outputNumber = parseFloat( numericString );
if ( outputNumber < settings.min ) return true;
/********************************
* Trims a string according to the settings provided
********************************/
function trimAlphaNum( inputString, settings ) {
if ( typeof inputString != 'string' ) return inputString;
var inChars = inputString.split( '' );
for ( i = 0; i < inChars.length; i++ ) {
var validatedStringFragment = outChars.join( '' );
if ( alphanum_allowChar( validatedStringFragment, Char, settings ) ) outChars.push( Char );
return outChars.join( '' );
function trimNum( inputString, settings ) {
if ( typeof inputString != 'string' ) return inputString;
var inChars = inputString.split( '' );
for ( i = 0; i < inChars.length; i++ ) {
var validatedStringFragment = outChars.join( '' );
if ( numeric_allowChar( validatedStringFragment, Char, settings ) ) outChars.push( Char );
return outChars.join( '' );
function removeUpperCase( inputString ) {
var charArray = inputString.split( '' );
for ( i = 0; i < charArray.length; i++ ) {
function removeLowerCase( inputString ) {
function isUpper( Char ) {
var upper = Char.toUpperCase();
var lower = Char.toLowerCase();
if ( (Char == upper) && (upper != lower) ) return true; else return false;
function isLower( Char ) {
var upper = Char.toUpperCase();
var lower = Char.toLowerCase();
if ( (Char == lower) && (upper != lower) ) return true; else return false;
function isCaseless( Char ) {
if ( Char.toUpperCase() == Char.toLowerCase() ) return true; else return false;
function getBlacklistSet( allow, disallow ) {
var setOfBadChars = new Set( BLACKLIST + disallow );
var setOfGoodChars = new Set( allow );
var blacklistSet = setOfBadChars.subtract( setOfGoodChars );
function getDigitsMap() {
var array = '0123456789'.split( '' );
for ( i = 0; i < array.length; i++ ) {
function getLatinCharsSet() {
var lower = 'abcdefghijklmnopqrstuvwxyz';
var upper = lower.toUpperCase();
var azAZ = new Set( lower + upper );