Custom Currency validation - Magento using validate.js
-
16-10-2019 - |
Question
I am looking for a way to all for custom currency validation using native magento validate.js
I have a form which allows users to add custom prices to a text field. I am using the following classes to validate the inputs:
validate-digits-range
digits-range-1-10
The problem is the text field is pre-populated with the currency sign £
so will not pass validation.
I have tried to adapt the validate-currency-dollar
class but the test still fails
Validation.add('validate-currency-dollar', 'Please enter a valid $ amount. For example $100.00.', function(v) {
// [$]1[##][,###]+[.##]
// [$]1###+[.##]
// [$]0.##
// [$].##
return Validation.get('IsEmpty').test(v) || /^\$?\-?([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})?)$/.test(v)
});
Has anyone done this before can share some advice please?
Solution
I have changed a bit regular expression
/^[\$£]?\-?([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})?)$/
Try it instead of standard ones.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange