PDA

View Full Version : Free JavaSccript codes



JavaScriptBank
03-10-2011, 11:13 PM
Helpful JavaScripts to develop website

JavaScript RegEx Example Code for Text Input Limitations

One more JavaScript code example (http://www.javascriptbank.com" title="JavaScript code example) to limit user inputs with many options: non-alphanumeric characters with spaces, removes ex... detail (http://www.javascriptbank.com/javascript-regex-example-code-text-input-limitations.html/en//) at JavaScriptBank.com - 2.000+ free JavaScript codes (http://www.javascriptbank.com/)


http://www.javascriptbank.com/javascript.images/form/javascript-regex-example-code-text-input-limitations.jpg (http://www.javascriptbank.com/javascript/form/javascript-regex-example-code-text-input-limitations/preview/en/)
Demo: JavaScript JavaScript RegEx Example Code for Text Input Limitations (http://www.javascriptbank.com/javascript-regex-example-code-text-input-limitations.html/en/)


How to setup

Step 1: Use JavaScript code below to setup the script
JavaScript

<script type="text/javascript">
// Created by: Ilya Gerasimenko | http://www.gerasimenko.com/
// This script downloaded from www.JavaScriptBank.com

// clean lines
cleanUpLine = function (str,limit) { // clean string
var clean_pass1 = str.replace(/\W/g, ' '); //replace punctuation with spaces
var clean_pass2 = clean_pass1.replace(/\s{2,}/g, ' '); // compact multiple whitespaces
var clean_pass3 = clean_pass2.replace(/^\s+|\s+$/g, ''); // trim whitespaces from beginning or end of string
var clean_pass4 = clean_pass3.substring(0,limit); // trim string
return clean_pass4;
}

// number of keywords and keyword length validation
cleanUpList = function (fld) {
var charLimit = 20; // ADJUST: number of characters per line
var lineLimit = 10; // ADJUST: number of lines
var cleanList = [];
var re1 = /\S/; // all non-space characters
var re2 = /[\n\r]/; // all line breaks
var tempList = fld.value.split('\n');
for (var i=0; i<tempList.length;i++) {
if (re1.test(tempList[i])) { // store filtered lines in an array
var cleanS = cleanUpLine(tempList[i],charLimit);
cleanList.push(cleanS);
}
}
for (var j=0; j<tempList.length;j++) {
if (tempList[j].length > charLimit && !re2.test(tempList[j].charAt(charLimit))) { // make sure that last char is not a line break - for IE compatibility
fld.value = cleanList.join('\n'); // restore from array
}
}
if (cleanList.length > lineLimit) {
cleanList.pop(); // remove last line
fld.value = cleanList.join('\n'); // restore from array
}
fld.onblur = function () {this.value = cleanList.join('\n');} // onblur - restore from array
}

// Multiple onload function created by: Simon Willison
// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

addLoadEvent(
function () {
document.form.word_list.onkeyup = function () {
cleanUpList(document.form.word_list);
}
}
);
</script>

Step 2: Copy & Paste HTML code below in your BODY section
HTML

Textarea below has limitations: 20 characters and 10 lines<br />
<form name="form">

<textarea cols="22" rows="12" name="word_list" id="word_list"></textarea>
</form>







JavaScript Countdown Timer (http://www.javascriptbank.com/javascript-countdown-timer.html/en/) - JavaScript Web based Music Player (http://www.javascriptbank.com/web-based-music-player.html) - JavaScript AJAX Page Content Loader
(http://www.javascriptbank.com/ajax-page-content-loader.html)


The Site may provide, or third parties may provide, links to non-JavaScriptBank.com Internet World Wide Web sites or resources. Because JavaScriptBank.com has no control over such sites and resources, you acknowledge and agree that JavaScriptBank.com is not responsible for the availability of such external sites or resources, and does not endorse and is not responsible or liable for any content, advertising, products, or other materials on or available from such sites or resources. You further acknowledge and agree that JavaScriptBank.com shall not be responsible or liable, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with use of or reliance on any such content, goods or services available on or through any such site or resource.