Forums

Currency validation: Comma instead of decimal point

kazpad 17 Oct, 2009
Hello all,
I am working on a form which needs some currency input (Euros in my case). In Germany it is standard to write 24,00 for a full twenty-four Euros.

I have checked an older post from December 2008 where it was suggested to use
/^\-?([1-9]{1}[0-9]{0,2}([., ][0-9]{3})*([.,][0-9]{0,2})?|[1-9]{1}\d*([, ][0-9]{0,2})?|0([.,][0-9]{0,2})?|([.,][0-9]{1,2})?) ?[$£€€]?$/
as regex string in 'components/com_chronocontact/js/validation.js'.

First of all, I do not have that file. Instead I only have 'components/com_chronocontact/js/jsvalidation2.js". When I tried to use the string, no validation worked. Then I added a comma to the end of the string, as I had seen that somewhere else. The validation for the other fields worked then, but not the currency.

My code in the mentioned file looks like this:
			if(field.hasClass('validate-currency-dollar')){

				if( tmessage) { var message_validate_currency_dollar = tmessage; }

				name.add( Validate.Format, {

					pattern:  /^\-?([1-9]{1}[0-9]{0,2}([., ][0-9]{3})*([.,][0-9]{0,2})?|[1-9]{1}\d*([, ][0-9]{0,2})?|0([.,][0-9]{0,2})?|([.,][0-9]{1,2})?) ?[$£€€]?$/,
					failureMessage: message_validate_currency_dollar 

				});


My questions now are this:
Do I have to add a parameter in the validation tab of the backend?
Has there been a major update in Chronoforms since that above mentioned post in 2008 and the string or location of the string has changed?

I am pretty sure I only need a minor change, but currently I seem to be looking for the needle in the heystack.
Any help?

Thanks to everybody.
kazpad
GreyHead 19 Oct, 2009
Hi kazpad,

The Validation that ChronoForms uses has changed since that post. Here's the form HTML for a mini-form with Validation that will accept 99 99.99 or 99,99
<?php
$script= "
var text_0_val = new LiveValidation('text_0', { validMessage: 'I am a valid amount !' });
text_0_val.add(Validate.Format, { 
  pattern: /^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$/i,
  failureMessage: 'I am NOT a valid amount' 
});
";
$doc =& JFactory::getDocument();
$script = "window.addEvent('domready', function() { $script });";
$doc->addScriptDeclaration($script);
?>
<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Amount</label>
    <input class="cf_inputbox" maxlength="150" size="30" title="" id="text_0" name="text_0" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="button_1" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>
The key part is the definition of $script near the beginning. The various values of text_0 in there should be changed to the id of your form input.

Bob
kazpad 19 Oct, 2009
Hello GreyHead,
I almost thought that there has been a change in approach for the validation, as even the filenames did not match any more.

If I understand your post correctly, your coding consists of a PHP part for the validation and an HTML of the actual formcode. I therefore gathered that the coding would have to go into the formcode. I did that and replaced the 'text_0' with the relevant id (in my case 'text_28'). Having done that, however, there is no validation for any of the fields. Is there something wrong with my transfer of the coding?



<?php
$script= "
var text_28_val = new LiveValidation('text_28', { validMessage: 'I am a valid amount !' });
text_28_val.add(Validate.Format, {
  pattern: /^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$/i,
  failureMessage: 'I am NOT a valid amount'
});
";
$doc =& JFactory::getDocument();
$script = "window.addEvent('domready', function() { $script });";
$doc->addScriptDeclaration($script);
?>

<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 370px;">Ich zahle einen jährlichen Mitgliedsbeitrag in Höhe von  €</label>
    <input class="cf_inputbox required validate-number validate-currency-dollar" maxlength="6" size="6" title="Der Eintrag von mindestens 24 € ist hier notwendig" id="text_28" name="Beitrag" value="24.00" type="text" />
 <a class="tooltiplink" onclick="return false;"><img height="16" border="0" width="16" class="tooltipimg" alt="" src="components/com_chronocontact/css/images/tooltip.png"/></a>
				<div class="tooltipdiv">Mindestbeitrag::  24,- €</div>  
  </div>
  <div class="cfclear"> </div>
</div>




In the past the validation entries needed to go into the validation file. I therefore then tried to put the PHP part into the "jsvalidation2.js'file without any success.

That is why I think your coding needs to go into the formcode. But why does it defunct the validation altogether?

😟

kazpad
GreyHead 19 Oct, 2009
Hi kazpah,

That code looks OK, and it works on my little test form.
[sendfb][/sendfb]
bob
GreyHead 19 Oct, 2009
Hi Kazpah,

I restored the Backup form here and all the validation appears to be working OK.

Maybe some other conflict with your template scripts??

Bob
kazpad 19 Oct, 2009
Hello GreyHead,
thanks a lot for your help!

Everything works fine now. I somehow assumed that it would be some kind of parameter which would be wrongly set. In the end it was quite a substantial one: In the process of my changes the 'Enable Validation' was not set any more.

I hope I can soon go live without bothering you too much any more.

😛

kazpad
GreyHead 19 Oct, 2009
Hi kazpad,

Hmmm . . . yes, that would stop the validation ! :-)

Bob
kazpad 19 Oct, 2009
Hello GreyHead,
I spoke too early. Having checked the results in Firefox, Chrome and IE8, I realize that FF and Chrome have no problems. IE, however, throws an error and therefore stops processing the validation.

The error reads "The object doesn't support this property or method" for livevalidation_standalone.js in line 82. The URI given is 'http://greyhead.net/components/com_chronocontact/js/livevalidatin_standalone.js'.

I assume the same error would occur for your implementation of my code in IE. If this is correct, then your coding would not be useful, as it seems to me that it is only using standard LiveValidation functionality.

Could you see whether your example works on IE or not.

Thanks

kazpad
GreyHead 20 Oct, 2009
Hi kazpad,

Hmmm . . . not so good. You can replace 'domready' in my code with 'onload' and the error messages go away and the basic validation works but the extra validation still doesn't work in IE7 or IE8.

I'm not sure where to go from there. I'll take another looks later and see if I have any bright ideas.

Bo
kazpad 20 Oct, 2009
Hello GreyHead,
thanks for the tip. As you said, everything else worked except that particular validation. But you got me googling for "mootools domready ie" and I found that apparently there is a problem with mootools using domready internally (should be fixed with rel. 2.0 https://mootools.lighthouseapp.com/projects/2706/tickets/729-domready-is-fired-after-load-in-all-ie-versions.

In some forums they say they had similar problems and fixed it by using 'load' instead of 'domready'. Not that I understand the funcionality behind it, as it is difficult to track, but I gave it a try and your validation and 'load' replacing 'domready' works now with FF, Chrome, Safari and IE (tested with IE6 and IE8).

If anybody wants to use it, this is the coding:

<?php
$script= "
var text_0_val = new LiveValidation('text_0', { validMessage: 'I am a valid amount !' });
text_0_val.add(Validate.Format, { 
  pattern: /^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$/i,
  failureMessage: 'I am NOT a valid amount' 
});
";
$doc =& JFactory::getDocument();
$script = "window.addEvent('load', function() { $script });";
$doc->addScriptDeclaration($script);
?>
<div class="form_item">
  <div class="form_element cf_textbox">
    <label class="cf_label" style="width: 150px;">Amount</label>
    <input class="cf_inputbox" maxlength="150" size="30" title="" id="text_0" name="text_0" type="text" />
  
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Submit" name="button_1" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>


Thanks for your help and effort

kazpad😛
GreyHead 20 Oct, 2009
Hi kazpad,

Ah - excellent! I was almost there - forgot to take the 'on' off the front of 'onload' :-(

The difference between domready and load is small, I think it is that load waits for all the page assets (graphics, etc) to load before it fires whereas domready fires as soon and the page html is loaded.

The whole purpose of the wrapper round the validation script is to load the JavaScript into the page header and to delay execution until the main page load is complete (otherwise you can get other script errors).

Bob
This topic is locked and no more replies can be posted.