I want to generate target textbox value depends on another textbox value with following conditions -
Assumed First TextBox value - "myfile name"
- If first textbox value not start with "Copy of " then target value will "
Copy of [1] myfile name
". - If first textbox value "
Copy of [1] myfile name
" then target value will "Copy of [2] myfile name
". - If first textbox value "
Copy of [2] myfile name
" then target value will "Copy of [3] myfile name
".
i.e increment of the no.
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);
}
});