2

Given that ccnum is a 16-character string containing only numbers (credit card number), I want to format the string nicely with 4 digits at a time, separated by spaces. I tried this:

ccnum=ccnum.substring(0, 4)+' '+
      ccnum.substring(4, 4)+' '+
      ccnum.substring(8, 4)+' '+
      ccnum.substring(12, 4);

But I get unpredictable results, such as:

12341234 1234 1234
1234 1234 12341234

Not sure why?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
BB Design
  • 695
  • 12
  • 26
  • Your substring is not [syntactically correct](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring) - you have coded it like [substr](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) – mplungjan Jul 30 '14 at 11:29
  • `substring` function splits a string and assigned to the same variable , check my answer for more info – vimal1083 Jul 30 '14 at 11:30

5 Answers5

4

You can use JavaScript's match() function together with a simple regular expression to get what you need.

The match() method retrieves the matches when matching a string against a regular expression.

var str = "1234123412341324";
var results = str.match(/\d{4}/g);
var final_cc_str = results.join(" ");

The regular expression /\d{4}/g is searching for digit characters (\d). Four of them ({4}). And it wants to find all matches so it uses the g flag for a global search.

After that I'm using the join() function to concatenate each of the matched elements with a space character as the delimiter.

The join() method joins all elements of an array into a string.

Lix
  • 47,311
  • 12
  • 103
  • 131
2
var ccnum = '1111222233334444';

ccnum.match(/.{1,4}/g);

Result

["1111", "2222", "3333", "4444"]
Earth
  • 3,477
  • 6
  • 37
  • 78
  • Assuming that this is user provided data, I don't think it would be wise to assume that the string only contains numbers... Unless there is some other validation method that was not mentioned by the OP. – Lix Jul 30 '14 at 11:31
  • If you know that you are searching for digits, I would say that it'll always be better to explicitly define what you want to search for. – Lix Jul 30 '14 at 11:32
  • Since credit card number is 16 digits which are numbers. – Earth Jul 30 '14 at 11:34
  • Yes, I am doing other validation to ensure 16 digits before I get to this step. – BB Design Jul 31 '14 at 12:12
2

You have confused

substring

The substring() method returns a subset of a string between one index and another, or through the end of the string.

Syntax str.substring(indexA[, indexB])

ccnum=ccnum.substring(0, 4)+' '+
      ccnum.substring(4, 8)+' '+
      ccnum.substring(8, 12)+' '+
      ccnum.substring(12, 16);

with substr

The substr() method returns the characters in a string beginning at the specified location through the specified number of characters.

Syntax str.substr(start[, length])

ccnum=ccnum.substr(0, 4)+' '+
      ccnum.substr(4, 4)+' '+
      ccnum.substr(8, 4)+' '+
      ccnum.substr(12, 4);
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

Use substr instead of substring

Try This

var ccnum = '123456789123456';

ccnum.substr(0, 4)+' '+ccnum.substr(4, 4)+' '+ccnum.substr(8, 4)+' '+ccnum.substr(12, 4);
vimal1083
  • 8,499
  • 6
  • 34
  • 50
1

Try this. JSFIDDLE

"1111222233334444".match(/.{1,4}/g).join(" ");
Anoop
  • 23,044
  • 10
  • 62
  • 76