0

How can I get the valid or invalid status of the postal code field in the Stripe Address Element

SS of the problem

I tried this code. but it has not worked. here is the code :

const postalCode = elements.getElement('postalCode');
const isInvalid = postalCode._invalid;
const isEmpty = postalCode._empty;
const isValid = !(isEmpty || isInvalid);
console.log(isValid);

Also, I tried this method too. but it's not returning the valid or invalid status of the field.

addressElement.on('change',(e)=>{
   console.log(e);
});

Is there anyway to get this status?

Thank you.

1 Answers1

0

You can access the postal code entered by the user like this:

addressElement.on('change', event => {
    console.log(event.value.address.postal_code);
});

You can't check if the postal code itself is valid, but you could check if the whole form is valid like this:

addressElement.on('change', event => {
    if (event.complete) {
       // Then the whole form is valid, including the postal code
    } else {
      // Some of the information is missing or incorrect
    }
})
soma
  • 1,332
  • 1
  • 2
  • 8