2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Field for displaying NZ GST numbers (usually 8-9 digits in the format ##-###-### or ##-###-####).
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-formattedinput
|
2007-07-19 12:40:28 +02:00
|
|
|
* @see http://www.ird.govt.nz/payroll-employers/software-developers/software-specs/
|
|
|
|
*/
|
|
|
|
class GSTNumberField extends TextField {
|
|
|
|
|
|
|
|
function jsValidation() {
|
|
|
|
$formID = $this->form->FormName();
|
2008-01-10 04:28:13 +01:00
|
|
|
$error = _t('GSTNumberField.VALIDATIONJS', 'Please enter a valid GST Number');
|
2007-07-19 12:40:28 +02:00
|
|
|
$jsFunc =<<<JS
|
|
|
|
Behaviour.register({
|
|
|
|
"#$formID": {
|
|
|
|
validateGSTNumber: function(fieldName) {
|
|
|
|
var el = _CURRENT_FORM.elements[fieldName];
|
|
|
|
if(!el || !el.value) return true;
|
|
|
|
|
|
|
|
var value = \$F(el);
|
|
|
|
if(value.length > 0 && !value.match(/^[0-9]{2}[\-]?[0-9]{3}[\-]?[0-9]{3,4}\$/)) {
|
2008-01-10 04:28:13 +01:00
|
|
|
validationError(el,"$error","validation",false);
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
|
|
|
Requirements::customScript($jsFunc, 'func_validateGSTNumber');
|
|
|
|
|
|
|
|
return "\$('$formID').validateGSTNumber('$this->name');";
|
|
|
|
}
|
|
|
|
|
|
|
|
function validate($validator){
|
|
|
|
$valid = preg_match(
|
|
|
|
'/^[0-9]{2}[\-]?[0-9]{3}[\-]?[0-9]{3,4}$/',
|
|
|
|
$this->value
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!$valid){
|
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
2007-10-25 04:47:45 +02:00
|
|
|
_t('GSTNumberField.VALIDATION', "Please enter a valid GST Number"),
|
2007-07-19 12:40:28 +02:00
|
|
|
"validation",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|