Javascript Library
 
  Here I provide a list of functions that pratically have been required for my works (both professional and private ones). Some of the following functions have been collected from the Internet and some have been made by myself. In the former case I mention the author of the original version eventually followed by my name just in case my modifications were significant.

  • trim(...) Trims the string representation of an object.
  • isPIvaValid(...) checks whether a string is conforming to the format of the Italian Partita IVA.
  • isCFValid(...) checks whether a string is conforming to the format of the Italian Codice Fiscale.
  • isDateValid(...) checks whether a date is conforming to the format d/m/2yyy.

trim

Description:
Returns a trimmed string derived from the object passed to it.
Parameters
Object o: the object
Returned value
The value trimmed.
Author
Giovanni Costagliola
Comments
Surprisingly for me, it seems that JavaScript's String class doesn't provide a useful method worldwide known as trim() so I got a little search on the web and, surprisingly again, I found a lot of different implementations, many of them ugly coded and partially working and, and in some circumstances, semantically incoherent. Stated that I decided to write it from scratch: this is my proposal. If you think I could do it better please email me.
/* 
 * Input:
 *   an object
 * Output:
 *   the trimmed value of the string representing the object
 *
 * (C) Giovanni Costagliola <jc@nexusat.it> under GPL.
 */
function trim(o)
{
	var re=/^(\s*)([\w\W]*[^\s])(\s*)$/;
	var rs=re.exec(String(o));
	return rs != null ? rs[2] : "";
}

isPIvaValid

Description:
Checks whether a string is conforming to the format of the Italian Partita IVA.
Parameters
String pi: the string representing the partita IVA
Returned value
The string OK upon success, otherwise a string describing the nature of the violation occurred. The last string is suitable to be prompted to the user.
Author
Unknown
function isPIvaValid(pi)
{
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
		   return "La partita IVA contiene un carattere non valido `" +
		      pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return "La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return 'OK';
}

isCFValid

Description:
Checks whether a string is conforming to the format of the Italian Codice Fiscale.
Parameters
String cf: the string representing the codice fiscale.
Returned value
The string OK upon success, otherwise a string describing the nature of the violation occurred. The last string is suitable to be prompted to the user.
Author
Unknown
function isCFValid(cf)
{
   var validi, i, s, set1, set2, setpari, setdisp;
   cf = cf.toUpperCase();
   if( cf.length != 16 )
      return "La lunghezza del codice fiscale non è\n"
         + "corretta: il codice fiscale dovrebbe essere lungo\n"
         + "esattamente 16 caratteri.\n";
   validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   for( i = 0; i < 16; i++ ){
      if( validi.indexOf( cf.charAt(i) ) == -1 )
         return "Il codice fiscale contiene un carattere non valido `" +
	    cf.charAt(i) +
	    "'.\nI caratteri validi sono le lettere e le cifre.\n";
   }
   set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   s = 0;
   for( i = 1; i <= 13; i += 2 )
      s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   for( i = 0; i <= 14; i += 2 )
      s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
      return "Il codice fiscale non è corretto:\n"+
         "il codice di controllo non corrisponde.\n";
   return 'OK';;
}

isDateValid

Description:
Checks whether a date is conforming to the format d/m/2yyy where:
  • d represents a day in both short or long format;
  • m represents a month in both short or long format;
  • 2yyy represents a year between 2000 and 2999.
Parameters
String theDate: the string representing the date
Returned value
The value true upon success, false otherwise.
Author
Giovanni Costagliola
Comments
I originally implemented this function to avoid the use of the Date object which is sensible of the locale settings. If you have a more elegant solution I'd like to read it so email me.
/* 
 * Input:
 *   a string representing a date in the format d/m/2yyy
 * Output:
 *   true to indicate a correct date, false otherwise
 *
 * (C) Giovanni Costagliola <jc@nexusat.it> under GPL.
 */
function isDateValid(theDate) {
	var re=/^([0-9]{1,2})\/([0-9]{1,2})\/(2[0-9]{3})$/;
	var res;
	
	if ((res = re.exec(theDate)) != null) {
		var d = res[1]-0;
		var m = res[2]-0;
		var y = res[3]-0;
		var ly = (y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0);
		var maxDay;
		
		switch (m) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				maxDay = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				maxDay = 30;
				break;
			case 2:
				if (ly)
					maxDay = 29;
				else
					maxDay = 28;
				break;
			default:
				return false;
				break;
		}
		
		if (1<=d && d<=maxDay)
			return true;
		else
			return false;	
	}
	else // The parse failed
		return false;
}

 


Valid XHTML 1.0!

Valid CSS!

home – e-mail
(C) 2003 by Giovanni Costagliola — All Rites Reversed