Replacing the radio buttons and checkboxes with custom images

20 April, 2006

function chgCB(){
  CHKB = document.getElementById('newCB').getElementsByTagName('input');
  for(i=0; i < CHKB.length; i++){
    if(CHKB[i].type == "checkbox"){
      IMG = document.createElement('img');
      CHKB[i].parentNode.insertBefore(IMG, CHKB[i]);
      IMG.src = "radio0.gif";
      if(CHKB[i].checked  == true) IMG.src = "radion.gif";
      if(CHKB[i].disabled == true) IMG.src = "radio.gif";
      else{ // Changing state's behaviours are only applied if the box is clickable
        CHKB[i].onchange = function(){
          IMG = this.parentNode.getElementsByTagName('img')[0];
          if(this.checked == true) IMG.src = 'radion.gif'
          else IMG.src = 'radio0.gif'
        }
        if(!window.sidebar){
          CHKB[i].parentNode.onclick = function(){
            CHK = this.getElementsByTagName('input')[0];
            CHK.checked = (CHK.checked == true) ? false : true;
            CHK.onchange()
          }
        }
      }
      CHKB[i].style.visibility = 'hidden';
      CHKB[i].style.position  = 'absolute';
    }
  }
}
function chgRB(){
  CHKB = document.getElementById('newRB').getElementsByTagName('input');
  for(i=0; i < CHKB.length; i++){
    if(CHKB[i].type == "radio"){
      IMG = document.createElement('img');
      CHKB[i].parentNode.insertBefore(IMG, CHKB[i]);
      IMG.src = "radio0.gif";

      IMG.id = 'img'+CHKB[i].id;
      IMG.relation = CHKB[i].name;

      if(CHKB[i].checked  == true) IMG.src = "radion.gif";
      if(CHKB[i].disabled == true) IMG.src = "radio.gif";
      else{ // Changing state's behaviours are only applied if the box is clickable
        CHKB[i].onchange = function(){

          IMG = document.getElementById('newRB').getElementsByTagName('img');
          for(i=0; i < IMG.length; i++){
            if(IMG[i].relation != this.name) continue;
            if(IMG[i].src.indexOf('radio.gif') != -1) continue;
            if(IMG[i].id == 'img'+this.id) IMG[i].src = 'radion.gif';
            else IMG[i].src = 'radio0.gif'
          }

        }
        if(!window.sidebar){
          CHKB[i].parentNode.onclick = function(){
            CHK = this.getElementsByTagName('input')[0];
            CHK.checked = (CHK.checked == true) ? false : true;
            CHK.onchange()
          }
        }
      }
      CHKB[i].style.visibility = 'hidden';
      CHKB[i].style.position  = 'absolute';
    }
  }
}


Check ‘Codice Fiscale’ – C#

19 April, 2006

C#

using System;
using
System.Text;

namespace Verifiche
{
    /// <summary>
    ///
Classe codice fiscale
    /// </summary>

    public
class CodiceFiscale
   {

        public
CodiceFiscale
()
       {
       }
      
       public
static bool CheckCodiceFiscale(string CodiceFiscale)
       {
   
            bool
result = false;
            const int caratteri = 16;
            if (CodiceFiscale == null)
                    return result;

            if (CodiceFiscale.Length < caratteri)
                    return PartitaIva.CheckPartitaIva(CodiceFiscale);

            // se il codice fiscale non è di 16 caratteri il controllo
            // è già finito prima ancora di cominciare

            if (CodiceFiscale.Length != caratteri)
                    return result;

            // stringa per controllo e calcolo omocodia
            const string omocodici = "LMNPQRSTUV";
            // per il calcolo del check digit e la conversione in numero
            const string listaControllo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            int[] listaPari = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
            int[] listaDispari = {1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23};

            CodiceFiscale = CodiceFiscale.ToUpper();
            char[] cCodice = CodiceFiscale.ToCharArray();

             // check della correttezza formale del codice fiscale
             // elimino dalla stringa gli eventuali caratteri utilizzati negli
             // spazi riservati ai 7 che sono diventati carattere in caso di omocodia
             for (int k = 6; k < 15; k++)
             {
                  if ((k == 8) || (k == 11))
                          continue;
                  int x = (omocodici.IndexOf(cCodice[k]));
                  if (x != -1)
                  cCodice[k] = x.ToString().ToCharArray()[0];
             }

            //  Regex rgx = new Regex(@"^[A-Z]{6}[]{2}[A-Z][]{2}[A-Z][]{3}[A-Z]$");
            //  Match m = rgx.Match(new string(cCodice));
            //  result = m.Success;
            result = true;
            // da una verifica ho trovato 3 risultati errati su più di 4000 
 codici fiscali 
            // ho temporaneamente rimosso il test con le Regular fino a quando non riuscirò a capire perchè in alcuni casi sbaglia         
           

             // normalizzato il codice fiscale se la regular non ha buon
             // fine è inutile continuare
             if (result)
             {
                  int somma = 0;
                 
// ripristino il codice fiscale originario 
                  // grazie a Lino Barreca che mi ha segnalato l'errore
                 
cCodice = CodiceFiscale.ToCharArray();
                  for (int i = 0; i < 15; i++)
                  {
                        char c = cCodice[i];
                        int x = "0123456789".IndexOf(c);
                        if (x != -1)
                             c = listaControllo.Substring(x,1).ToCharArray()[0];
                        x = listaControllo.IndexOf(c);
                        // i modulo 2 = 0 è dispari perchè iniziamo da 0
                        if ((i % 2) == 0)
                               x = listaDispari[x];
                        else
                               x = listaPari[x];
                        somma += x;
                  }

                  result = (listaControllo.Substring(somma % 26,1) == CodiceFiscale.Substring(15,1));
             }
             return result;
        }

    }

}


Check ‘Codice Fiscale’ – VB.NET

19 April, 2006

VB.NET

Imports System
Imports System.Text
Imports System.Text.RegularExpressions

Namespace Verifiche

     Public Class CodiceFiscale
            Public Sub New()
            End Sub

         Public Shared Function CheckCodiceFiscale(ByVal CodiceFiscale As String) As Boolean
 
              Dim result As Boolean = False
               Const
caratteri As Integer = 16
        
               If
CodiceFiscale Is Nothing Then
                    Return
result
               End If

               If CodiceFiscale.Length < caratteri Then
                    Return
PartitaIva.CheckPartitaIva(CodiceFiscale)
               End If

               If Not (CodiceFiscale.Length = caratteri) Then
                    Return
result
               End If

              Const omocodici As String = "LMNPQRSTUV"
              Const listaControllo As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

               Dim listaPari As Integer() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
               Dim listaDispari As Integer() = {1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23}

               CodiceFiscale = CodiceFiscale.ToUpper
               Dim cCodice As Char() = CodiceFiscale.ToCharArray

               Dim k As Integer = 0

               For k = 6 To 14
                    If (k = 8) OrElse (k = 11) Then
                    Else
                         Dim
x As Integer = (omocodici.IndexOf(cCodice(k)))
                         If Not (x = -1) Then
                              cCodice(k) = x.ToString.ToCharArray(0)
                         End If
                    End If
               Next

               'Dim rgx As Regex = New Regex("^[A-Z]{6}[]{2}[A-Z][]{2}[A-Z][]{3}[A-Z]$")
               'Dim
m As Match = rgx.Match(New String(cCodice))
               'result = m.Success
                result = True

               If result Then
                   Dim
somma As Integer = 0
                   cCodice = CodiceFiscale.ToCharArray
                   Dim i As Integer = 0
                   For i = 0 To 14
                        Dim c As Char = cCodice(i)
                        Dim x As Integer = "0123456789".IndexOf(c)
                        If Not (x = -1) Then
                            c = listaControllo.Substring(x, 1).ToCharArray(0)
                        End If
                        x = listaControllo.IndexOf(c)
                        If (i Mod 2) = 0 Then
                              x = listaDispari(x)
                        Else
                              x = listaPari(x)
                        End If         
                        somma += x
                  
Next
                   result = (listaControllo.Substring(somma Mod 26, 1) = CodiceFiscale.Substring(15, 1))
             End If
             Return
result
        End Function

    End Class
End Namespace


Disable or Enable Text Fields by clicking a CheckBox – JAVASCRIPT

19 April, 2006

function UsernamePasswordBox() {
var value = document.getElementById(‘login_as_guest’).checked;
if (value == true){
document.getElementById(‘login’).disabled=true;
document.getElementById(‘pass’).disabled=true;
}
else{
document.getElementById(‘login’).disabled=false;
document.getElementById(‘pass’).disabled=false;
}
}

In Body:

Login:

<input class=”text” name=”login” id=”login” value=”" type=”text” title=”">

Password:

<input class=”password” name=”pass” id=”pass” value=”" type=”password” title=”">

Login as a guest: <input name=”login_as_guest” id=”login_as_guest” type=”checkbox” title=”" onclick=”UsernamePasswordBox()”>


Enable and Disable Form Elements – Javascript

7 April, 2006

This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.

Here is a standard text field:

Now here it is again, disabled:

Try typing in the second box, and you'll see that you can't – it's been disabled. You can do the same any other elements in a form – checkboxes, selection boxes and the rest.

Disabling Form Elements – Common Uses

One common use of this is to only enable the element, once the appropriate field has been selected earlier in the form.

Now here's a practical example. The 'explain your choice' field only becomes enabled when I have selected the answer to Question 1. Before selecting a radio button, try typing in the text field – you can't!

Question 1

Answer a:
Answer b:
Explain your choice:

Disabling Form Elements – How it's done

To disable an element, put the phrase disabled="true" within the element tag.

Once you've set the disabled tag, you can enable it using something like the following code:

<script language="javascript">

function enableField()
{
document.form1.address2.disabled=false;
}

</script>

<a href="javascript:enableField()">Click here to enable the element<a/>
 

With disabled set to false, the form element becomes useable.