Archive for the ‘JavaScript’ Category.

Allow only numbers or letters in HTML inputs

What about forcing the user to type a number/letter in the input you want.

With just a bit of JavaScript we can control this what is the user typing int he input.

Allow only numbers:
(also dot and del)

function isNumberKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode;
 
	if((charCode==46||charCode==8||charCode==45||charCode==47) ||(charCode >= 48 && charCode <= 57) ){
		return true;
	}
	else {
		return false;
	}		
}

And to use it in a input would be like this for example:

<input id="math"  type="text"  onkeypress="return isNumberKey(event);" />

Try it: How much is 20+10?

Allow only letters:
(also space, del, enter)

function isAlphaKey(evt){
	var charCode = (evt.which) ? evt.which : event.keyCode;
	 if ((charCode==231 || charCode==199) || (charCode==241 || charCode==209) ||(charCode==8 || charCode==32) || ( (charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122) ) ) {
	 	return true;
	 }
	 else {
		 return false;
	 }
}

And to use it in a input would be like this for example:

<input id="name" type="text"  onkeypress="return isAlphaKey(event);"  />

Try it: Your name:

I know should be easier, but just wait a bit for HTML 5 that is going to make our life better. For sure ;)

IT Excuses Generator

Some times we really need to fool people to get more time to work, or just some of you want to play more video games…

For all of us we can always use the IT excuses generator ;)

IT excuses generator

To use it in your site c&p this:

<script type="text/javascript" src="http://lab.neo22s.com/ITexcuses/itexcuses.js"></script>
<div id="display_excuse" style="cursor:pointer;" onclick="displayExcuse();">When does the project end?</div>

Example:

When does the project end?


Other excuses generators:

jsSelect – Multiple select value modifier

Last day I show you how to change the value of a select in html, but what if you need to change many at same time?

I did this function to handle it:

Demo | Download

function changeSelectValues(theController,theElements) {
	theElements = theElements.split(','); 
	for(var z=0; z<theelements .length;z++){
		theItem = document.getElementById(theElements[z]);
		if(theItem.type){
			if (theItem.type=='select-one') {
				theItem.value=theController.value;
				}
		} 
		else {
			theInputs = theItem.getElementsByTagName('select');
			for(var y=0; y<theInputs.length; y++){
				if(theInputs[y].type == 'select-one' && theInputs[y].id != theController.id){
					theInputs[y].value = theController.value;
				}
			}
		} 
  	}
}

In the first param, normally you would write this. Second parameter is a list delimited by coma (“,”) to specify from where you want to change the selects.

Example to change all the selects from a div call “selects”: Continue reading ‘jsSelect – Multiple select value modifier’ »

Set value for Select – JavaScript

This JavaScript function allows you to set the value of a select tag without having to know its position in the list.

function setValueSelect(SelectName, Value) {
     SelectObject = document.getElementById(SelectName);
     for(index = 0;  index < SelectObject.length;  index++) {
            if(SelectObject[index].value.toLowerCase() == Value.toLowerCase()) SelectObject.selectedIndex = index;
      }
}

Usage Example:

<select id="clang">
<option value="arabic">Arabic</option>
<option value="english">English</option>
<option value="french">French</option>
<option value="persian">Persian</option>
<option value="turkish">Turkish</option>
</select>
setValueSelect("clang", "persian");

This selects the persian value in the select.

isArray JavaScript

Simple JavaScript function that returns true if is an array or false if it’s not ;)

function isArray(obj) {
     if (obj.constructor.toString().indexOf("Array") == -1)
	      return false;
     else return true;
}
Follow me