• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Need to rewrite a javascript in another format (to make it compatible with Joomla Breezing Forms)

I am trying to create a custom validation in Breezing forms for a 16-character field which would handle people's Fiscal Code (exists only in Italy). It is composed of a mix of letters and numbers. I found a working script somewhere on the internet which the author is using on an html form he has designed.

jQuery.validator.addMethod("cfvalido", function(value, element) {
var cf = value.toUpperCase();
var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
if (!cfReg.test(cf))
return false;
var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
var 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 false;
return true;
}, "Insert a valid Fiscal Code");

Open in new window


I need to rewrite this script in a format the Breezing Forms can process and whose output it can accept.
Below is an example of a code that validates if a dollar amount has been input in a field. It also checks if a maximum of two decimal places is entered (the cents part is optional as you can see):
function ff_dollaramount2dp(element, message)
{
    var ex  = /^\d+$|^\d+\.\d{2}$/;
    if (!ex.test(element.value)) {
        if (message=='') message = element.name+" must be a number with two decimal places.\n";
        ff_validationFocus(element.name);
        return message;
    } // if
    return '';
} // ff_dollaramount2dp

Open in new window



Please help me change the first code into the second format so that I might use it with Breezing forms.

Thanks in advance
0
badwolfff
Asked:
badwolfff
  • 2
  • 2
1 Solution
 
Rainer JeschorCommented:
Hi,
sorry have no experience in Breeze Forms but from your sample the following adoption should work:
function ff_fiscalCode(element, message)
{
	if (message=='') message = element.name+" must be a valid Fiscal Code.\n";
    var ex  = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
    if (!ex.test(element.value.toUpperCase())) {
        ff_validationFocus(element.name);
        return message;
    } // if

	var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	var 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 != element.value.toUpperCase().charCodeAt(15)-'A'.charCodeAt(0) )
	{
        ff_validationFocus(element.name);
        return message;
	} else {
		return '';
	}
} // ff_fiscalCode

Open in new window

HTH
Rainer
0
 
badwolfffAuthor Commented:
Hi,

thanks. It sort of works but not really.

You can test it here:
http://www.maisondelajoie.com/sostienici.html

If you click on the blue submit button at the bottom of the page (with "invia" written on it) without filling the form, you will get a series of errors above the form among which the last one is relevant to us:
Campo "Codice Fiscale" obbligatorio o valore inserito incorretto (I generate this using Breezing Forms manager)

Now the problem is this. If I put in a 16-digit number (the field takes a maximum of 16 digits) that is in the wrong format (type any random combination of numbers and letters) I get to see the error above. Just fill in that field and submit the form to see. Try any of these codes, they are all valid:
GNLGNI77E19Z614P
MRCKTY75D22F257A
PTKFTN01A41Z404C

The moment I put in a correct code, without filling the rest of the form, I not only do not see the error mentioned above, all other errors for all the other compulsory fields also go away for some reason.

Could you please help fix this? If the correct code is input, then only THAT error message should disappear, all the rest should still show if they are empty.

thanks
0
 
Michel PlungjanIT ExpertCommented:
Rainor missed something.

Try

function ff_dollaramount2dp(element, message) {
    var ex  = /^\d+$|^\d+\.\d{2}$/;
    if (!ex.test(element.value)) {
        if (message=='') message = element.name+" must be a number with two decimal places.\n";
        ff_validationFocus(element.name);
        return message;
    } // if
    if (!validateFiscal(element.value) {
        if (message=='') message =  "Insert a valid Fiscal Code\n";
        ff_validationFocus(element.name);
        return message;
    }
    return "";
} // ff_dollaramount2dp

function validateFiscal(value) {
  var cf = value.toUpperCase();
  var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
  if (!cfReg.test(cf)) return false;
  var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
  var 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 false;
  return true;
}

Open in new window

0
 
badwolfffAuthor Commented:
Hi Michael,

thanks for the code. I think you got the wrong idea. I don't want to do two validations at once. I just want to validate the Fiscale Code. The problem is that the validation script for the fiscal code I only have it in jQuery format as you can see. And breezing forms takes scripts only in the format that I showed above. What I needed was someone's help in converting the jQuery code to conform to the breezing forms format as shown in the example.

I tried your code and with it on, no errors are shown at all and it does not work. Could you please fix your code?

thanks
0
 
Michel PlungjanIT ExpertCommented:
There was a missing ) in my code - there is no jQuery at all in my code.

Fixed code:

function ff_dollaramount2dp(element, message) {
    var ex  = /^\d+$|^\d+\.\d{2}$/;
    if (!ex.test(element.value)) {
        if (message=='') message = element.name+" must be a number with two decimal places.\n";
        ff_validationFocus(element.name);
        return message;
    } // if
    if (!validateFiscal(element.value)) {
        if (message=='') message =  "Insert a valid Fiscal Code\n";
        ff_validationFocus(element.name);
        return message;
    }
    return "";
} // ff_dollaramount2dp

function validateFiscal(value) {
  var cf = value.toUpperCase();
  var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
  if (!cfReg.test(cf)) return false;
  var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
  var 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 false;
  return true;
}

Open in new window


A guess at how to ONLY do the fiscal

function ff_fiscaltest(element, message) {
    if (element.value.length<16) {
        if (message=='') message = element.name+" must be a valid 16 digit fiscal number.\n";
        ff_validationFocus(element.name);
        return message;
    } // if
    if (!validateFiscal(element.value)) {
        if (message=='') message =  "Insert a valid Fiscal Code\n";
        ff_validationFocus(element.name);
        return message;
    }
    return "";
} // ff_fiscal

function validateFiscal(value) {
  var cf = value.toUpperCase();
  var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
  if (!cfReg.test(cf)) return false;
  var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
  var 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 false;
  return true;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now