Validate user input

DocsCurrent

Last updated: Feb 28th, 7:43pm

Validate payer input when payment buttons are clicked. For example, your page might contain a web form that must be validated before starting the transaction.

For an optimal payer experience, complete validation before rendering the buttons. For validation that you can't complete before rendering the buttons, run synchronous or asynchronous validation when clicking the PayPal buttons.

Know before you code

Required
Get Started

Complete the steps in Get started to get the following sandbox account information from the Developer Dashboard:

  • Your personal and business sandbox accounts.
  • Your access token.
Get started

Required
Standard Checkout

This feature modifies an existing standard Checkout integration and uses the following:

Optional
Explore PayPal APIs with Postman

You can use Postman to explore and test PayPal APIs. Learn more in our Postman guide.

Synchronous validation

Use synchronous validation when possible because it provides a better user experience. However, you may need to use asynchronous validation for asynchronous channels or server-side validation.

    1<p id="error" class="hidden">Please check the checkbox</p>
    2<label><input id="check" type="checkbox"> Check here to continue</label>
    3<script>
    4 paypal.Buttons({
    5 // onInit is called when the button first renders
    6 onInit: function(data, actions) {
    7 // Disable the buttons
    8 actions.disable();
    9 // Listen for changes to the checkbox
    10 document.querySelector('#check')
    11 .addEventListener('change', function(event) {
    12 // Enable or disable the button when it is checked or unchecked
    13 if (event.target.checked) {
    14 actions.enable();
    15 } else {
    16 actions.disable();
    17 }
    18 });
    19 },
    20 // onClick is called when the button is clicked
    21 onClick: function() {
    22 // Show a validation error if the checkbox is not checked
    23 if (!document.querySelector('#check').checked) {
    24 document.querySelector('#error').classList.remove('hidden');
    25 }
    26 }
    27 }).render('#paypal-button-container');
    28</script>

    Asynchronous validation

    Some validation must be done on the server-side or using an asynchronous channel. Don't use asynchronous validation for any validation that can be done synchronously because it degrades the payer's experience.

      1<p id="error" class="hidden">Please check your information to continue</p>
      2<script>
      3 paypal.Buttons({
      4 // onClick is called when the button is clicked
      5 onClick: function(data, actions) {
      6 // Return a promise from onClick for async validation
      7 return fetch('/my-api/validate', {
      8 method: 'post',
      9 headers: {
      10 'content-type': 'application/json'
      11 }
      12 }).then(function(res) {
      13 return res.json();
      14 }).then(function(data) {
      15 // If there is a validation error, reject, otherwise resolve
      16 if (data.validationError) {
      17 document.querySelector('#error').classList.remove('hidden');
      18 return actions.reject();
      19 } else {
      20 return actions.resolve();
      21 }
      22 });
      23 }
      24 }).render('#paypal-button-container');
      25</script>

      We use cookies to improve your experience on our site. May we use marketing cookies to show you personalized ads? Manage all cookies