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
badwolfffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.