jessepollak / card

:credit_card: make your credit card form better in one line of code
MIT License
11.65k stars 1.48k forks source link

How to remove the input whitespaces when entering a card number? #485

Open stanleynik opened 3 years ago

stanleynik commented 3 years ago

But I would like to keep the whitespaces in the card

melloware commented 3 years ago

There is nothing built in so you will have to code this yourself in Javascript

NikhilMali-98 commented 9 months ago

To remove whitespaces from a card number input in a programming language like JavaScript, we can use the replace function to replace all whitespace characters with an empty string.

Example : // Assuming cardNumber is the input containing whitespace let cardNo = "1234 5678 9012 3456";

// Remove whitespaces let cleanedCardNo = cardNumber.replace(/\s/g, '');

console.log(cleanedCardNo);

In this example, the regular expression \s matches any whitespace character, and the g flag ensures a global search, replacing all occurrences. The result is a card number without any whitespaces.