over 11 years ago
How to give space after 4 characters to an TextBox input as well as retrict alphabets.
We have a TextBox as :-
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>
<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.
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)