WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Adding a Filter to a Text Element
Categories : Java Script, Data Validation, Form Processing Click here to Update Your Picture
Joyce Babu
Date : Jul 02nd 2006
Grade : 4 of 5 (graded 5 times)
Viewed : 28649
File : 4437.zip
Images : No Images for this code example.
Search : More code by Joyce Babu
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 

This script can be used for adding a filter to a textbox or textarea. You can decide whether to allow only numeric, alphabetic, alphanumeric characters in a textbox/textarea. You can also add custom characters to the allowed set of characters.

Filter.js
/*==========================================================================#
# * Function for adding a Filter to an Input Field                          #
# * @param  : [filterType  ] Type of filter 0=>Alpha, 1=>Num, 2=>AlphaNum   #
# * @param  : [evt         ] The Event Object                               #
# * @param  : [allowDecimal] To allow Decimal Point set this to true        #
# * @param  : [allowCustom ] Custom Characters that are to be allowed       #
#==========================================================================*/
function filterInput(filterType, evt, allowDecimal, allowCustom){
    var keyCode, Char, inputField, filter = '';
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var num   = '0123456789';
    // Get the Key Code of the Key pressed if possible else - allow
    if(window.event){
        keyCode = window.event.keyCode;
        evt = window.event;
    }else if (evt)keyCode = evt.which;
    else return true;
    // Setup the allowed Character Set
    if(filterType == 0) filter = alpha;
    else if(filterType == 1) filter = num;
    else if(filterType == 2) filter = alpha + num;
    if(allowCustom)filter += allowCustom;
    if(filter == '')return true;
    // Get the Element that triggered the Event
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget;
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow
    if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27) )return true;
    // Get the Pressed Character
    Char = String.fromCharCode(keyCode);
    // If the Character is a number - allow
    if((filter.indexOf(Char) > -1)) return true;
    // Else if Decimal Point is allowed and the Character is '.' - allow
    else if(filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)return true;
    else return false;
}



Usage Example
<!doctype html public "-//w3c//dtd html 4.1 transitional//en">
<html>
<head>
<title>Input Filter</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<meta name="MSSmarttagsPreventParsing" content="TRUE" />
<meta name="robots" content="index,follow" />
<script type="text/javascript" src="filter.js"></script>
<style>
body, table{font:normal 12px Verdana, Arial, sans-serif;}
.tblTest td{background:#F4F4F9;border:1px #C0C0C0 solid;padding:5px 15px;}
.tblTest td span.comment{color:#999999;font-size:10px;font-style:italic;}
.inputField{width:240px;}
</style>
</head>

<body bgcolor="#ffffff">
<table style="height:100%;width:100%;">
<tr><td>

<center><form name="frmTest">
<table class="tblTest" cellspacing="2" cellpadding="0" style="">
<tr><td colspan="2" style=""><b>Input Filter</b></td></tr>
<tr><td>Numbers Only<br><span class="comment">(No Decimal Point)</span></td><td><input class="inputField" type="text" onKeyPress="return filterInput(1, event)" name="numOnly" value="0123456789"></td></tr>
<tr><td>Numbers Only<br><span class="comment">(With Decimal Point)</span></td><td><input class="inputField" type="text" onKeyPress="return filterInput(1, event, true)" name="numOnly" value="56789.01234"></td></tr>
<tr><td>Alphabets Only</td><td><textarea class="inputField" onKeyPress="return filterInput(0, event)" name="alphaOnly1">ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz</textarea></td></tr>
<tr><td>Alphabet Numeric</td><td><textarea class="inputField" onKeyPress="return filterInput(2, event)" name="alphaNumeric">ABCDEFGHIJKLMnopqrstuvwxyz
0123456789</textarea></td></tr>
<tr><td>Alphabet Numeric<br><span class="comment">(With Custom characters [ .])</span></td><td><textarea class="inputField" onKeyPress="return filterInput(2, event, false, ' .')" name="alphaNumCustom1">Test this...</textarea></td></tr>
<tr><td>Alphabet Numeric<br><span class="comment">(With Custom characters [@ _ .])</span></td><td><textarea class="inputField" onKeyPress="return filterInput(2, event, false, '@_-.')" name="alphaNumCustom2">some_body@some-domain.com</textarea></td></tr>
</table></form>
</center>
</td></tr></table>
</body>
</html>



Javascript URL and Email Validation
Categories : Java Script, Data Validation, Form Processing, Email, URLs
Form Processing : with alert Highlight field name which is not filled by user
Categories : Java Script, Form Processing, Data Validation, Beginner Guides, Web Design
Changing the Style of form objects using the JavaScript OnClick method.
Categories : Java Script, Form Processing, Beginner Guides, CSS
Balance values between two list boxes. Change the value of list box A acording to the value in list box B and vice versa using Javasvript.
Categories : Java Script, Form Processing
Show hide table rows to make dynamic forms
Categories : Beginner Guides, Java Script, Form Processing, HTTP
JavaScript dropdown list menu to switch any page.
Categories : Java Script, Beginner Guides, Form Processing
Validating a URL with JavaScript RegExp
Categories : Java Script, Data Validation, Beginner Guides
Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP, Form Processing, PHP Classes, Data Validation, Beginner Guides
Form Validation Using PHP to highlight non valid fields
Categories : PHP, Form Processing, Data Validation, Beginner Guides
Form validations using javascript and including all validations in one message
Categories : HTML and PHP, Java Script, Form Processing
PHP and javascript mouseover, mouseout, and mousedown events
Categories : PHP, Java Script, Form Processing, Beginner Guides
showing Help (assistance) to the user while filling html forms.
Categories : HTML, Java Script, Form Processing
Conditional Check - a script that allows a user to submit a form only if the user check a checkbox.
Categories : HTML, Java Script, Form Processing, Beginner Guides
Hilight Form Element onFocus
Categories : Java Script, Form Processing, CSS
Upload any fixed type of files, control file type through javascript and encrypt filename using php so file not get overwrite
Categories : PHP, Java Script, Functions, PHP References, Form Processing
 m d wrote :1824
Pretty cool, I needed to allow users submit their posts/threads using the apostrophe (') if needed.
So I added a little adjustment like this;
var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\''; //notice the \' part added in.

Now this allows for the apostrophe if needed.

Cheers