Reputation: 81
I need to add comma after end of the string in textarea and length of the string will be 10 digit + 1(',') using javascript
currently what I'm getting is
1234567890
1234567890
1234567890
Here is my code JSFIDDLE
what I need is something like
1234567890,
1234567890,
1234567890
Thanks in advance.
$('.number').keyup(function () {
this.value = this.value
.replace(/[\n\r]+/g, "")
.replace(/(.{10})/g, "$1\n");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="number" style="width: 200px; height: 200px;">
</textarea>
Upvotes: 0
Views: 243
Reputation: 464
You can add code for comma as below.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="number" style="width: 200px; height: 200px;">
</textarea>
<script src="jquery-2.2.3.min.js"></script>
<script>
$('.number').keyup(function () {
this.value = this.value
.replace(/[,\n]/g, "")
.replace(/(.{10})/g, "$1,\n");
});
</script>
Upvotes: 0
Reputation: 13988
Just add additional comma in your replace statement. No need to add one more replace with the code as @Abdullah Shoaib mentioned.
$('.number').keyup(function () {
this.value = this.value
.replace(/[\n,\r]+/g, "")
.replace(/(.{10})/g, "$1,\n");
});
Snippet
$('.number').keyup(function () {
this.value = this.value
.replace(/[\n,\r]+/g, "")
.replace(/(.{10})/g, "$1,\n");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="number" style="width: 200px; height: 200px;">
</textarea>
Upvotes: 1
Reputation: 1662
Try this code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="number" style="width: 200px; height: 200px;">
</textarea>
<script src="jquery-2.2.3.min.js"></script>
<script>
$('.number').keyup(function () {
var txt=$(this).val();
txt=$.trim(txt);
var artmp=txt.split("\n");
var len=artmp.length;
var lstline=artmp[len-1];
if(lstline.length>9){
lstline=lstline+',\n';
}
artmp[len-1]=lstline;
$('.number').val(artmp.join("\n"));
});
</script>
Upvotes: 0