Web Analytics Made Easy -
StatCounter Putting spaces into a number - CodingForum

Announcement

Collapse
No announcement yet.

Putting spaces into a number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Putting spaces into a number

    Can anyone suggest the simplest/most efficient way to add spaces into a 16-digit (credit card) number, if not already present, so that

    1234123412341234

    is changed to 1234 1234 1234 1234

    Regex does not seem to help here!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #2
    Hello,

    I'm sure this is not the most efficient or the simplest way to do this but it does seem to work. Perhaps you will find it usefull.

    Code:
    <script type="text\javascript">
    
    function makeSpaces()
    {
    	var num='1234567891234567';
    	var subStrings=new Array();
    	j=0;
    
    	for(i=0;i<num.length;i=i+4)
    	{
    		var str=num.substring(i,i+4);
    		subStrings[j]=str;
    		j++;
    	}
    	
    	var str2="";
    
    	for(k=0;k<subStrings.length;k++)
    	{
    		str2+=subStrings[k]+" ";
    	}
    
                    alert(str2);
    }
    
    makeSpaces();
    
    </script>
    Hope That Helps,
    Basscyst
    Helping to build a bigger box. - Adam Matthews

    Comment


    • #3
      Here's another way.
      Code:
      <script type="text/javascript">
      str = '1234567898765432';
      temp = '';
      while(str.length >= 4){
      temp += str.substring(0,4)+' ';
      str = str.substring(4,str.length);
      }
      alert(temp)
      </script>
      .....Willy

      Edit: Ooops
      I meant 4 not 5.
      str = str.substring(4,str.length);
      Last edited by Willy Duitt; Mar 3, 2004, 03:12 PM.

      Comment


      • #4
        It almost works. . . .

        Code:
        <script type="text/javascript">
        str = '1234567898765432';
        temp = '';
        while(str.length > 0){
        temp += str.substring(0,4)+' ';
        str = str.substring(4,str.length);
        }
        alert(temp)
        
        </script>
        Should be >0 or it drops the last 4.

        Basscyst
        Helping to build a bigger box. - Adam Matthews

        Comment


        • #5
          It works now. I fixed it.
          That'll teach me to assume my codes work.

          .....Willy

          Comment


          • #6
            Thank you Willy and Basscyst!

            All the code given in this post has been tested and is intended to address the question asked.
            Unless stated otherwise it is not just a demonstration.

            Comment


            • #7
              Now that your problem is solved, you realize that not all credit cards are 16 digits in length, right?

              American Express is 15 digits. Diners Club is 14 and 15 digits. Various European cards have various lengths from 13 to (I think) 18 digits.
              Steven Sommers (blog)
              Shift4 Corporation -- www.shift4.com

              Creators of $$$ ON THE NET(tm) payment processing services.

              Comment


              • #8
                We don't take AMEX or Diners which are charge cards. All European credit cards are 16 digits, no exceptions. The 13-digit cards were phased out some years ago. Some Switch (debit cards) are 18 or 19 digits. Yes, I have catered for them, also when the user has entered the number with spaces already included.

                if (card length ==16) {add spaces after each 4 digits}
                if (cardnumber.substring (0,1) == 6) {is Switch card so add spaces after each three digits}
                else {cardnumber already has spaces- do nothing}

                Not perfect, but the aim is to assist human readability by
                breaking up the long number. The card number is already validated of course.

                But thanks for your comments! Your advertising plug has been duly noted - again.

                By the way, did you know that if a credit card number includes 0 and 9 in sequence the number is still a valid number (i.e passes the Luhn formula test) if these two digits are reversed? This has actually happened to us, and due to a clerk misreading the number the wrong card was charged. That is why I am trying to improve legibility.
                Last edited by Philip M; Mar 12, 2004, 02:43 AM.

                All the code given in this post has been tested and is intended to address the question asked.
                Unless stated otherwise it is not just a demonstration.

                Comment


                • #9
                  Yea, the luhn-mod-10 formula is not the strongest version of a lrc that could have been used, but it's the standard and I doubt the card companies will ever change to another form of validation. On paper, it should stop 9 out of 10 miskeys. In reality, it seems to stop only about 7 out of 10 miskeys.
                  Steven Sommers (blog)
                  Shift4 Corporation -- www.shift4.com

                  Creators of $$$ ON THE NET(tm) payment processing services.

                  Comment

                  Working...
                  X