1

I have tried with all solution. Actually, i want to add number validationmeans anyone will not able to write characters in text-field.

when I see in the browser by inspect element then getting this code.

<input class="input-text" type="text" data-bind="
value: value,
valueUpdate: 'keyup',
hasFocus: focused,
attr: {
    name: inputName,
    placeholder: placeholder,
    'aria-describedby': noticeId,
    id: uid,
    disabled: disabled
}" name="postcode" placeholder="" aria-describedby="notice-HGXM9L3" id="HGXM9L3">

if we can also add any custom class then we can set validation by jquery. anyone have an idea for that, please.

  • Have you checked below my answer for the issue? – Suresh Chikani Jan 9 '18 at 6:56
  • Yes i have checked with your code, but still allow to enter character. i have copied layout file form /vendor/magento/module-checkout/view/fontend/layout/checkout_index_index.xml to /app/design/frontend/themename/Magento_Checkout/layout/ – Ravi Varma Jan 9 '18 at 8:48
3

Here I am sharing code for add custom validation to postcode field

checkout_index_index.xml file code for postcode field.

<item name="postcode" xsi:type="array">
    <!-- post-code field has custom UI component -->
    <item name="component" xsi:type="string">Magento_Ui/js/form/element/post-code</item>
    <item name="validation" xsi:type="array">
        <item name="required-entry" xsi:type="boolean">true</item>
    </item>
</item>

You need to add below code for number validation

<item name="validate-digits" xsi:type="string">true</item>

Final postcode field code

<item name="postcode" xsi:type="array">
    <!-- post-code field has custom UI component -->
    <item name="component" xsi:type="string">Magento_Ui/js/form/element/post-code</item>
    <item name="validation" xsi:type="array">
        <item name="required-entry" xsi:type="boolean">true</item>
        <item name="validate-digits" xsi:type="string">true</item>
    </item>
</item>

Note: You have to override checkout_index_index.xml in your theme.

  • Yes i have checked with your code, but still allow to enter character. i have copied layout file form /vendor/magento/module-checkout/view/fontend/layout/checkout‌​_index_index.xml to /app/design/frontend/themename/Magento_Checkout/layout/ – Ravi Varma Jan 9 '18 at 8:49
  • check it with core file. I think your file is not properly override. – Suresh Chikani Jan 9 '18 at 8:51
  • which core files? can show path or more guide please. – Ravi Varma Jan 9 '18 at 8:52
  • vendor/magento/module-checkout/view/frontend/layout/checkout_index_index.xml – Suresh Chikani Jan 9 '18 at 8:53
  • I checked by core files, it show your code it show validation message when anyone enter text. but i want user can not write text means user will be not allow to write text, if user write any then he can write only number in zipcode field. – Ravi Varma Jan 9 '18 at 9:26
0

Default magento 2 doesn't provide validation like anyone will not able to write characters in text-field. So you should go with custom jquery or HTML 5.

Please check below URL: https://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery

Hope above solution work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.