You must Sign In to post a response.
  • Category: ASP.NET

    Credit card no validation

    I have a texbox for credit card no ........i want when client enter ccno in text box 1234 then automatically - added after four character like this 1234-1234-1234-1234
  • #762659
    Hi

    Make autopostback=true for textbox and in the text changed event write below code.

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    int i = int.Parse(TextBox1.Text);
    string s = i + "-" + i + "-" + i + "-" + i;
    TextBox1.Text = s;
    }

    find the attachment, as soon as you leave first textbox you will get the required text in ccno.

    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment

  • #762665
    it not the way i m looking

  • #762674
    Then how you are looking for.

    If you want in jquery use the below code.

    <script src="jquery-2.1.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    $('#TextBox1').focusout(function () {
    var s = $(this).val();
    var ss = s + "-" + s + "-" + s + "-" + s;
    $('#TextBox1').val(ss);
    });
    });
    </script>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>

    Sridhar Thota.
    Editor: DNS Forum.

  • #762680
    Hai Guddu,
    You can use MaskedTextbox to achieve your requirements. You can specify the format so that it will be called based on the formatting changes of the text.
    You can get the exmaple below at msdn:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx

    http://stackoverflow.com/questions/5460962/using-string-format-for-a-credit-card-number

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #762696
    when i enter card no. in textbox then 1234 and - added automatically 1234-4569-3756-4253 like this

  • #762707

    Hello Guddu,

    Refer the below code :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    <script type="text/javascript">
    function masking(input, textbox, e) {
    if (input.length == 4 || input.length == 9 || input.length == 14) {
    if (e.keyCode != 8) {
    input = input + '-';
    }
    textbox.value = input;
    }
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    Credit Card No :
    <asp:TextBox ID="txtCardNumber" runat="server" CssClass="inputField" Style="width: 240px;" MaxLength="19"
    onblur="masking(this.value,this,event);"
    onKeyPress="masking(this.value,this,event);"></asp:TextBox>
    </div>
    </form>
    </body>
    </html>


    See attached image. It's an output of this code.

    Hope this will help you.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762708
    Hi

    This will meet your requirement.
    Set TextBox max length to 18.

    Use below jQuery code snippet.

    <script src="jquery-2.1.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    $('#TextBox1').keyup(function () {
    var s = $(this);
    if (((s.val().length + 1) % 5) == 0)
    {
    s.val(s.val() + "-");
    }
    });
    });
    </script>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server" MaxLength="18"></asp:TextBox>
    </div>
    </form

    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment


Sign In to post your comments