I'm trying to make the text of an input be formatted by dividing the 48 numbers into blocks of 6 numbers, for example:
00000000000000000000000000000000000000000000
becomes
000000-000000-000000-000000-000000-000000-000000-000000
These numbers are returned with a $response after submitting a form with CURL.
For some reason when the result returns it is not in this format, I need to erase a number and rewrite it so that it is corrected and in the correct format.
This is the code I'm using:
JS
$(window).on("load",
function () {
$("#resID").on("change paste input", function (e) {
var t = this.value.replace(/\D/g, "");
48 == t.length ? (this.value = t.match(new RegExp(".{1," + t.length / 6 + "}", "g")).join("-")) : (this.value = t)
});
});
HTML
<div class="response">
<?php if($response != ''){ ?>
<input type="search" class="form-control" id="resID" style="text-align: center;" value="<?php echo $response; ?>" data-clipboard-target="#resID" href="#" readonly>
<?php } ?>
</div>
In short, it is formatting correctly but I need to update the numbers by deleting the last one and typing it in again.
Does anyone know what to be wrong?
My Answer:
To insert an element into an array in JavaScript, you can use the `splice()` method. Here's an example:
javascript
let fruits = ["apple", "banana", "cherry", "date"];
fruits.splice(2, 0, "orange"); // Insert "orange" at index 2
console.log(fruits); // Output: ["apple", "banana", "orange", "cherry", "date"]
In this example, the `splice()` method is used to insert the element "orange" at index 2 in the `fruits` array. The first argument specifies the index at which to start inserting elements, the second argument specifies the number of elements to remove (in this case, 0), and the third argument specifies the element to insert.
Rate this post
4 of 5 based on 8695 votesComments