How to give space after 4 characters to an TextBox input as well as retrict alphabets.
We have a TextBox as :-
<asp:TextBox ID="txtCardNumber" class="cardnumber" runat="server"
onkeyup="GiveSpace(this,4)" MaxLength="20" ></asp:TextBox>
Write the javascript function as :-
<script type="text/javascript">
function GiveSpace(textboxid, spaceafter) {
spaceafter = spaceafter|| 4;
var v = textboxid.value.replace(/[^\dA-Z]/g, ''),
reg = new RegExp(".{" + spaceafter+ "}", "g")
textboxid.value = v.replace(reg, function (a, b, c) {
return a + ' ';
});
}
</script>
This will restrict alphabets as well as give a space after every 4 numbers we enter in a textbox.
Note:- Very useful on the payment page when we ask for the credit card number.
0 Comment(s)