0

I need a code generator in jQuery that when clicking on the button it appears in the input text a number of 13 digits, start in number 7.

This is the code in my view:

<div class="form-group <?php echo !empty(form_error('barcode')) ? 'has-error':'';?>">
    <label for="barcode">Barcode:</label>
    <div class="input-group barcode">
        <div class="input-group-addon">
            <button class="fa fa-barcode" title="Generate Random Barcode"></button>
        </div>
        <input type="text" class="form-control" id="barcode" name="barcode" required value="<?php echo set_value('barcode');?>">
    </div>
    <?php echo form_error("barcode","<span class='help-block'>","</span>");?>
 </div>

This is the input text + button:

enter image description here

The generated number like this:

enter image description here

Alon Eitan
  • 11,997
  • 8
  • 49
  • 58
WilsonicX
  • 141
  • 1
  • 8
  • You'll use Javascript for this. JQuery is just what you use to make the page change. Try starting by just making a random number generator, then implementing the barcode stuff later. – David Culbreth Oct 25 '18 at 17:55
  • I don't understand, you're showing us the generated number which means that you know how to implement this – Alon Eitan Oct 25 '18 at 17:55
  • I only need genera a number of 13 digits, and show this in the input text – WilsonicX Oct 25 '18 at 18:02
  • 1
    I removed the duplicate because I think that [this answer](https://stackoverflow.com/a/1349426/754119) would be more useful. It seem like you don't need 13 digits, but 12 because the first number is always 7. You can define the possible combination to be only digits by changing `possible` in that answer and also change `i < 5` to `i < 11` to get a random 12 digits combination – Alon Eitan Oct 25 '18 at 18:12

1 Answers1

0

You can do this :

$(document).on('click', '#barcode_btn', function(e){
   e.preventDefault();
   var val1 = Math.floor(100000 + Math.random() * 999999);
   var val2 = Math.floor(10000 + Math.random() * 99999);
   $('[name="barcode"]').val('7 '+val1+' '+val2);
})
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
  <form>
    <input type="text" name="barcode" value="">
    <button id="barcode_btn">Generate Barcode</button>
  </form>

</body>
</html>
Santu Roy
  • 197
  • 7