Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Bank account validation demo using Postcoder</h1>

<p>This pen uses a demo API key which returns the same fixed data for any combination of sort code and account number.</p>

<p>For real results, replace the demo API key with your own one in the JS. Not got an API key? <a href="https://postcoder.com/sign-up" target="_blank">Start a free Postcoder trial</a> and then <a href="https://admin.postcoder.com/account-settings/features" target="_blank">enable</a> the bank validation feature.</p>

<form>
  <label for="txt_sortcode">Sort code</label>
  <input type="text" id="txt_sortcode" value="100000" />

  <label for="txt_accountnumber">Account number</label>
  <input type="text" id="txt_accountnumber" value="31510604" />

  <button id="btn_submit">Submit</button>
</form>

<p>Download the <a href="https://postcoder.com/docs/sample-code/bank-validation" target="_blank">full code example</a> or explore the <a href="https://postcoder.com/docs/bank-validation/bank" target="_blank">API reference</a> by <a href="https://postcoder.com" target="_blank">Postcoder</a>.</p>
</p>
              
            
!

CSS

              
                /* Heading style */
h1 {
  font-size: 1.17rem;
}

/* Form labels */
label {
  display: block;
  margin-block: 1.25rem 0.25rem;
}

/* General form inputs */
input {
  display: block;
  box-sizing: border-box;
  width: 100%;
  padding: 10px;
  height: 40px;
  margin-block-end: 0.5rem;
  border: solid lightgray 1px;
}

/* Default button styles */
button {
  padding: 0.5rem 1rem;
  border: none;
  background-color: #292929;
  color: #fff;
  font-family: sans-serif;
  font-weight: bold;
  margin-block-start: 0.5rem;
}

/* Bank account validation results */
form pre {
  display: none;
  background-color: #eee;
  border: dashed #ccc 1px;
  padding: 0.75rem;
  white-space: pre-wrap;
}

form pre.show {
  display: block;
}

/* Set the body tag defaults */
body {
  font-family: sans-serif;
}

              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function () {
  new PostcoderBank({
    apikey: "PCW45-12345-12345-1234X",
    sortcode: "#txt_sortcode", // query selector of the sortcode input field
    accountnumber: "#txt_accountnumber", // query selector of the accountnumber input field
    submitbtn: "#btn_submit" // query selector of the submit button
  });
});

class PostcoderBank {
  constructor(config) {
    this.config = config;
    this.init();
  }

  init() {
    this.endpoint =
      "https://ws.postcoder.com/pcw/" + this.config.apikey + "/bank/";

    this.sortcode_input = document.querySelector(this.config.sortcode);
    this.accountnumber_input = document.querySelector(
      this.config.accountnumber
    );
    this.submit_btn = document.querySelector(this.config.submitbtn);

    // add click event listener to the submit button, to call the search function
    this.submit_btn.addEventListener("click", this.search);

    // add a result container to the page, delete when not required.
    this.resultcontainer = document.createElement("pre");
    this.submit_btn.closest("form").appendChild(this.resultcontainer);
  }

  search = (event) => {
    event.preventDefault();

    if (this.sortcode_input.value && this.accountnumber_input.value) {
      let self = this;
      let sortcode = this.sortcode_input.value;
      let accountnumber = this.accountnumber_input.value;

      fetch(this.endpoint, {
        method: "post",
        headers: {
          "Content-Type": "application/json"
        },
        body: `{"sortcode":"${sortcode}","accountnumber":"${accountnumber}"}`
      })
        .then((response) => {
          if (!response.ok) {
            throw response;
          }
          return response.json();
        })
        .then((data) => {
          self.processResult(data);
        })
        .catch((err) => {
          if (typeof err.text === "function") {
            err.text().then((errorMessage) => {
              console.log(
                "Postcoder request error " + err.status + " : " + errorMessage
              );
            });
          } else {
            console.log(err);
          }
        });
    }
  };

  processResult = (result) => {
    // show result on page, delete when not required.
    this.resultcontainer.classList.add("show");
    this.resultcontainer.innerHTML =
      "Result: \n\n" + JSON.stringify(result, null, 4);
  };
}

              
            
!
999px

Console