Javascript: controllo caratteri speciali.

Scritto il 12-01-2011 da Vito Antonio Bonardi

Pubblicato su : Programming

0

function valida_carattere(object){
//var iChars = "!@#$£%^&*()+=-[]\\\;,./{}|\":<>?òèéàùì°§_";
var iChars =
           "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM’ ";
for (var i = 0; i < object.value.length; i++) {
    //utilizzare questa riga se il controllo va sui caratteri speciali
    //if (iChars.indexOf(object.value.charAt(i)) != -1) {
    //alert ("i= "+i+" e carattere= "+object.value.charAt(i));
    if (iChars.indexOf(object.value.charAt(i)) == -1) {
        alert ("Il campo contiene caratteri speciali. I caratteri speciali verranno rimossi.");
        object.value =
object.value.substring(0,i)+object.value.substring(i+1,object.value.length);
        return false;
    }
}
}

Javascript insertAdjacentElement, insertAdjacentHTML, insertAdjacentText Cross Browser Compatibility.

Scritto il 17-02-2009 da Vito Antonio Bonardi

Pubblicato su : Programming

4

Chi come me sviluppa software per il web, quotidianamente si trova ad affrontare problemi di compatibilità tra i vari browser.
Il problema che ho riscontrato in questi giorni, riguarda la funzione insertAdjacentHTML, metodo non standard di casa Microsoft (non è presente in ECMAscript).
Dopo una breve ricerca su google (santo google), ho trovato questa faq, che risolve il problema.
Ecco la soluzione:

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){

HTMLElement.prototype.insertAdjacentElement =
        function(where,parsedNode)
	{
	   switch (where){
		   case 'beforeBegin':
			   this.parentNode.insertBefore(parsedNode,this)
			   break;
		   case 'afterBegin':
		           this.insertBefore(parsedNode,this.firstChild);
			   break;
		   case 'beforeEnd':
			   this.appendChild(parsedNode);
			   break;
		   case 'afterEnd':
			   if (this.nextSibling)
			       this.parentNode.insertBefore(parsedNode,this.nextSibling);
			   else this.parentNode.appendChild(parsedNode);
			   break;
	  }
	}

     HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
     {
        var r = this.ownerDocument.createRange();
        r.setStartBefore(this);
        var parsedHTML = r.createContextualFragment(htmlStr);
        this.insertAdjacentElement(where,parsedHTML)
     }

     HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
     {
       var parsedText = document.createTextNode(txtStr)
       this.insertAdjacentElement(where,parsedText)
     }
}

function getObjInnerText(obj){
     if (document.all)
     { // IE;
       return obj.innerText;
     }
     else{
           if (obj.textContent){
	      return obj.textContent;
           }
}