HTML CSS JavaScript code for online payments

HTML/CSS, JavaScript code for online payments

To accept online payments on your website, you’ll need to use a combination of HTML, CSS, and JavaScript. Here’s an example of how you might set up a simple payment form using these technologies:


<form id="payment-form">
  <label for="card-number">Card Number:</label>
  <input type="text" id="card-number" name="card-number">

  <label for="expiry-date">Expiry Date:</label>
  <input type="text" id="expiry-date" name="expiry-date">

  <label for="cvv">CVV:</label>
  <input type="text" id="cvv" name="cvv">

  <label for="amount">Amount:</label>
  <input type="text" id="amount" name="amount">

  <button type="submit">Pay</button>

This HTML code creates a simple form with input fields for the user to enter their card number, expiry date, CVV, and the amount they want to pay.

#payment-form {
  width: 400px;
  margin: 0 auto;
  text-align: left;

#payment-form label {
  display: block;
  margin-bottom: 0.5em;
  font-size: 1.2em;

#payment-form input {
  width: 100%;
  padding: 0.5em;
  font-size: 1.2em;

#payment-form button {
  width: 100%;
  padding: 0.5em;
  font-size: 1.2em;
  background-color: #0074d9;
  color: white;
  border: none;
  cursor: pointer;

This CSS code styles the payment form and its elements, giving them appropriate sizes, padding, and colors.

const paymentForm = document.getElementById('payment-form');

paymentForm.addEventListener('submit', event => {
  event.preventDefault(); // prevent the form from being submitted

  const cardNumber = document.getElementById('card-number').value;
  const expiryDate = document.getElementById('expiry-date').value;
  const cvv = document.getElementById('cvv').value;
  const amount = document.getElementById('amount').value;

  // validate the input fields
  if (!cardNumber || !expiryDate || !cvv || !amount) {
    alert('Please fill out all the fields');

  // submit the payment information to your server or payment gateway

This JavaScript code listens for the form's submit event and prevents it from being submitted. It then retrieves the values of the input fields and performs some basic validation. Finally, it submits the payment information to your server or payment gateway.

Keep in mind that this is just a basic example, and there are many additional steps involved in implementing a secure online payment system. You'll need to use a payment gateway or processor to handle the actual payment processing, and you'll need to ensure that your website is secure and compliant with relevant regulations and industry standards.

Leave a Comment

Your email address will not be published. Required fields are marked *