0

I have an input type text field in my system.xml . I'd like this field to be validated in a way that the user can put numbers (0 to 9) and dots but not commas nor any other symbol.

In magento, I have the class "validate-number", that doesn't discriminate against commas versus dots. Same for "validate-digits": with this one, I can only put numbers, not dots.

Is there a way to validate decimal numbers with only dots as decimal separator?

2
  • If I understand your question, this is the same problem as your previous question magento.stackexchange.com/q/370177/45441 Commented Oct 10, 2023 at 11:44
  • they're related but they're not the same.. agreed that I could have posted the two question in a single post as one is the alternative of the other: does a proper field type for price exists? If not, is there a validation we can use to validate prices? The answer to both is no: there is no ready type for price field and no, there is no ready validation to use. Commented Oct 18, 2023 at 8:41

1 Answer 1

3

Follow below steps to add Custom Validation To System Configuration Field

1.add a field on system.xml file

<field id="your_field_id" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1"> <label>Your Field Label</label> <validate>validate-number-and-dot</validate> </field> 

2.Create requirejs-config file requirejs-config.js in app/code/Vendor/Module/view/adminhtml folder with the following code.

var config = { config: { mixins: { 'mage/validation': { 'Vendor_Module/js/validation': true } } } }; 

3.Create js file validation.js in app/code/Vendor/Module/view/adminhtml/web/js folder with the following code.

define([ 'jquery' ], function ($) { 'use strict'; return function (target) { $.validator.addMethod( 'validate-number-and-dot', function (value) { return /^[0-9.]+$/.test(value); }, $.mage.__('"Please enter numbers (0-9) and dots (.) only."') ); return target; }; }); 
1
  • so there's no an already existing validation, I have to create one.. thanks for the steps, they're really helpful Commented Oct 18, 2023 at 8:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.