beginerdeveloper
beginerdeveloper

Reputation: 845

How to add a hyphen in a TextBox input in ASP.NET?

I have an ASP.NET textbox where I want to add a hyphen in the textbox input if the length of the input is > 5. For example, if I type 123456789, the entry should show like this: 12345-6789

How to do this?

<asp:Button ID="Button1" runat="server" Text="Button" />

Upvotes: 0

Views: 1146

Answers (2)

PeterKA
PeterKA

Reputation: 24638

$('.phone').on('input', function() {
  this.value.length < 5 || this.value.charAt(5) == '-' || 
    $(this).val( [this.value.slice(0,5), '-', this.value.slice(5)].join('') );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name="phone" class="phone"/>

Upvotes: 2

Veverke
Veverke

Reputation: 11358

  var value = $("#your-textbox-id-here").val();

  if (value.length >= 5) {
      value = value.substring(0, 5) + "-" + value.substring(5);
      //console.log(value);
  }

Upvotes: 0

Related Questions