2

I want to generate target textbox value depends on another textbox value with following conditions -

Assumed First TextBox value - "myfile name"

  1. If first textbox value not start with "Copy of " then target value will "Copy of [1] myfile name".
  2. If first textbox value "Copy of [1] myfile name" then target value will "Copy of [2] myfile name".
  3. If first textbox value "Copy of [2] myfile name" then target value will "Copy of [3] myfile name".

i.e increment of the no.

Here is my code sample

How do I do this using jquery?

HTML:

<div class="panel">
  <input type="text" id="filename" value="filename">
</div>
<div class="panel">
  <input type="text" id="newfilename" value="">
</div>
<div class="panel">
  <button type="button" id="btnGenerate">
    Get File Name
  </button>
</div>

JS:

$('#btnGenerate').click(function(){
  var newname;
    var oldname = $('#filename').val();
  if(typeof oldname !== undefined || oldname != ''){
    if(oldname.indexOf('Copy of')){
            var nCount = parseInt(oldname.match(/\([.*]\)/),10) + 1;
            newname = "Copy of [" + nCount[1] + "] " + oldname;
            $('#newfilename').val(newname); 
    }else{
        newname = "Copy of [1] " + oldname'
    }
    $('#newfilename').val(newname);

  }
});
Mouser
  • 13,132
  • 3
  • 28
  • 54
Daniel Smith
  • 1,626
  • 3
  • 29
  • 59

4 Answers4

0

I've made the following adjustments to your code:

  if(oldname.substr(0, 7) == "Copy of"){
    var nCount = parseInt(oldname.match(/\[(\d+?)\]/)[1],10) + 1;
    var newname = oldname.replace(/\[\d+?\]/, "["+nCount+"]");
    $('#newfilename').val(newname);
  }
  else
  {
    $('#newfilename').val("Copy of [1] " + oldname); //prepend Copy of [1]
  } 

Look at oldname.substr(0, 7) == "Copy of" the former indexOf skipped the if, not executing the code.

now [(\d+?)\] does the magic. It takes out all numbers between [ and ] and groups them in (). \d+? means get all numbers that occur 1 or n times. ? makes it lazy so it stops at ].

var newname = oldname.replace(/\[\d+?\]/, "["+nCount+"]") simply do a replace on the [] block and replace it with nCount.


You can even optimize it using just a replace:

    var newname = oldname.replace(/\[(\d+?)\]/, function(whole, group1){
        return "[" + (parseInt(group1, 10)+1) + "]";
    });

$('#btnGenerate').click(function(){
 var oldname = $('#filename').val();
    if(typeof oldname !== undefined || oldname != ''){
      if(oldname.substr(0, 7) == "Copy of"){
        var newname = oldname.replace(/\[(\d+?)\]/, function(whole, group1){
         return "[" + (parseInt(group1, 10)+1) + "]";
        });
        $('#newfilename').val(newname); 
      }
      else
      {
       $('#newfilename').val("Copy of [1] " + oldname); //prepend Copy of [1]
      }     
  }
});
.panel{
  display:block;
  padding: 5px 10px;
}
.panel input{
  width:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel">
  <input type="text" id="filename" value="Copy of [1] filename">
</div>
<div class="panel">
  <input type="text" id="newfilename" value="">
</div>
<div class="panel">
  <button type="button" id="btnGenerate">
    Get File Name
  </button>
</div>

On a whole different subject. You are building a filesystem kind of control. Wouldn't it be better that the copy amount is being determined by the amount of occurrences of that file name (and copies)?

Community
  • 1
  • 1
Mouser
  • 13,132
  • 3
  • 28
  • 54
0

names =  new Array()
$('#btnGenerate').click(function(){
 var oldname = $('#filename').val();
  names.push(oldname);
  if(oldname != ''){
    
var nCount  = $.grep(names, function (elem) {
    return elem === oldname;
}).length;
      var newname = "Copy of [" +nCount + "] " + oldname;
   $('#newfilename').val(newname);  
  }
 
});
.panel{
  display:block;
  padding: 5px 10px;
}
.panel input{
  width:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel">
  <input type="text" id="filename" value="Name">
</div>
<div class="panel">
  <input type="text" id="newfilename" value="">
</div>
<div class="panel">
  <button type="button" id="btnGenerate">
    Get File Name
  </button>
</div>

I hope this is what you are looking for.

Vikram Sangat
  • 136
  • 11
0

Updated fiddle.

You're so close, you've just to adjust the regex a little bitand add else statement :

$('#btnGenerate').click(function(){
  var oldname = $('#filename').val();
  if(typeof oldname !== undefined || oldname != ''){

    //Adding '>-1' in this condition
    if(oldname.indexOf('Copy of')>-1) 
    { 
      var nCount = parseInt(oldname.match(/\[(.*)\]/)[1],10) + 1;
      var file_name = oldname.split('] ')[1];

      var newname = "Copy of [" + nCount + "] " + file_name;
    }else{
      var newname = 'Copy of [1] '+oldname;
    }

    $('#newfilename').val(newname); 
  }
});

Hope this helps.

$('#btnGenerate').click(function(){
  var oldname = $('#filename').val();
  if(typeof oldname !== undefined || oldname != ''){
    if(oldname.indexOf('Copy of')>-1){
      var nCount = parseInt(oldname.match(/\[(.*)\]/)[1],10) + 1;
      var file_name = oldname.split('] ')[1];

      var newname = "Copy of [" + nCount + "] " + file_name;
    }else{
      var newname = 'Copy of [1] '+oldname;
    }

    $('#newfilename').val(newname); 
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel">
  <input type="text" id="filename" value="Copy of [1] filename">
</div>
<div class="panel">
  <input type="text" id="newfilename" value="">
</div>
<div class="panel">
  <button type="button" id="btnGenerate">
    Get File Name
  </button>
</div>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
0

Your code is working, but you have to change the regex from

 /\([.*]\)/ to /\d+/
maheshiv
  • 1,778
  • 2
  • 17
  • 21