Home » Asp.netRSS

Update JavaScript

I have this JavaScript which can format the 16 digit card no in this format A12E-D624-SAB1-992D

function FormatCardNo(BenefCardCntrl)
    {
            
        var test = new RegExp("[^0-9,a-z,A-Z]", "g");
        var txt;
        var txtlength=0;
        var txtReturn;
        var field = document.getElementById(BenefCardCntrl);               
        txt = field.value.replace(test,"");
        txt = txt.replace(",","");
        txt = txt.toUpperCase();
        txtlength = parseInt(txt.length);
        if(txtlength > 16 )
        {
            txtReturn = txt.substr(0,4) + "-" +  txt.substr(4,4) + "-" + txt.substr(8,4) + "-" + txt.substr(12,4);
        }
        else if(txtlength > 12 && txtlength <= 16 )
        {
            txtReturn = txt.substr(0,4) + "-" +  txt.substr(4,4) + "-" + txt.substr(8,4) + "-" + txt.substr(12,4);
        }
        else if(txtlength > 8 && txtlength <= 12 )
        {
            txtReturn = txt.substr(0,4) + "-" +  txt.substr(4,4) + "-" + txt.substr(8,4);
        }
        else if(txtlength > 4 && txtlength <= 8 )
        {
            txtReturn = txt.substr(0,4) + "-" +  txt.substr(4,4);
        }
        else if(txtlength > 0 && txtlength <= 4 )
        {
            txtReturn = txt;
        }
        else
        {
            txtReturn = txt;
        }
        field.value = txtReturn ;
    }


I have some problem with this JavaScript

i.    I can not use left or Right arrow key from the keyboard.
ii.    If I try to replace a single character, it delete complete card no.
iii.    If I type all 16 digit card no and delete any no from the center, it reshuffle the card no and move the pointer to last.


I uploaded the application, please download it for testing.

http://cid-5ddb0edab545c264.office.live.com/browse.aspx/For-Alls?uc=1

Thankyou.

 

6 Answers Found

 

Answer 1

If anyone have any idea, please help me.

 

Answer 2

I think you should be using a switch statement, rather than using multiple else ifs.

Doing this would make your code much easier to read/debug.


cheers

 

Answer 3

csharp_learner:

I think you should be using a switch statement, rather than using multiple else ifs.

Doing this would make your code much easier to read/debug.


cheers


Switch statements are not meant to do ranges of numbers. What a bad idea.

 

Answer 4

lol - Who says. that comes down to one's references.

For me a switch statement is easier on the eye, as its broken down into blocks.

 

Answer 5

csharp_learner:
For me a switch statement is easier on the eye, as its broken down into blocks.

Okz, but I am not good in js, can you please update this as per my request.

 

Answer 6

Can you please update this as per my request.

 
 
Search for  Update JavaScript
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter