/    Sign up×
Community /Pin to ProfileBookmark

Need result to be a specific number of characters

What I’m looking to do with the following form is when a user inputs a certain length width and depth the output will be set to a certain number of characters. Basically looking to add a zero before an input of a number lower than 10 in two fields (product_number & product_id)

For example if a user inputs:
Length: 20.25 Width: 5.625 Depth: 9.125

the output I would like to have would be:

product_number: RSC_20.25_05.63_09.13

product_id: 1203056091

Right now the output looks like this:

product_number: RSC_20.25_5.63_9.13

product_id: 12035691

The reason for this is so when I sort my products in my shop they will go in the correct numerical order as well as making everything easier to maintain. I only need it to be 2 digits before the decimal point because the max number input will never be over 99…

Here is the code:

[CODE]<html>
<script>
Number.prototype.eighths= function(prec, ret){
prec= prec || 8;
while(prec%4)++prec;
var i= Math.floor(this),
s= i? i +’-‘:”,
num= this-i, div= 1/prec, mod= prec/4,
mag= Math.pow(10, Math.log(prec*2)/Math.log(2)),
f1= Math.round(mag*(num/div))/mag,
f= Math.round(f1);
if(f1-f> div/4) f= Math.ceil(f1);
if(f== 0) return i;
if(f== prec) return i+1;
if(ret=== 1) return s+f+’/’+prec;
if(f*2== prec) return s+’1/2’;
while(mod>= 2){
if(f%mod=== 0) return s+ f/mod+’/’+prec/mod;
mod/= 2;
}
return s+f+’/’+prec;
}
function calculate(){
var F=document.frmRSC,

l= Number(F.txtlength.value);
w= Number(F.txtwidth.value);
d= Number(F.txtdepth.value);

Q = Number(F.txtqty.value) /* Quantity */

Lrounded = (Math.ceil(l/.125)/8)
Wrounded = (Math.ceil(w/.125)/8)
Drounded = (Math.ceil(d/.125)/8)

F.product_number.value= ‘RSC_’+Lrounded.toFixed(2)+’_’+Wrounded.toFixed(2)+’_’+Drounded.toFixed(2);

F.product_id.value= ‘1’ + (Lrounded.toFixed(1)*10) + ” + Wrounded.toFixed(1)*10 + ” + Drounded.toFixed(1)*10

}
</script>
<form name= “frmRSC”>
<table width=”100%” border=”1″><td>

<div class=”ccms_form_element cfdiv_text” id=”txtlength_container_div”><label for=”txtlength”>txtlength</label><input

id=”txtlength” maxlength=”150″ size=”30″ class=” validate[‘required’,’number’]” title=”” label_over=”0″ hide_label=”0″ type=”text”

value=”” name=”txtlength” />
<div class=”clear”></div><div id=”error-message-txtlength”></div></div>

<div class=”ccms_form_element cfdiv_text”

id=”txtwidth_container_div”><label for=”txtwidth”>txtwidth</label><input id=”txtwidth” maxlength=”150″ size=”30″ class=”

validate[‘required’,’number’]” title=”” label_over=”0″ hide_label=”0″ type=”text” value=”” name=”txtwidth” />
<div class=”clear”></div><div id=”error-message-txtwidth”></div></div>

<div class=”ccms_form_element cfdiv_text”

id=”txtdepth_container_div”><label for=”txtdepth”>txtdepth</label><input id=”txtdepth” maxlength=”150″ size=”30″ class=”

validate[‘required’,’number’]” title=”” label_over=”0″ hide_label=”0″ type=”text” value=”” name=”txtdepth” />
<div class=”clear”></div><div id=”error-message-txtdepth”></div></div>
</td></table>

<table width=”100%” border=”1″><td>

<p>product_number <input type= “text” name= product_number size= 25 value= “”></p>
<br><br>

<input type= “hidden” name= category_id size= 25 value= “”>

<p>product_id <input type= “text” name= product_id size= 25 value= “”></p>
<br><br>

<input type= “hidden” name= product_full_image size= 25 value= “”>

Quantity: <INPUT TYPE = “Text” NAME = txtqty SIZE = 5 value =””>
<br><br>

<p><input type= “button” name= “b1” value= “Convert” onClick= “calculate()”></p>

</td>
</table>
</form>

[/CODE]

thanks,
Matt

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKEROct 29.2011 — Just added a padLeft function...
<i>
</i>&lt;html&gt;
&lt;script type="text/javascript"&gt;
Number.prototype.eighths= function(prec, ret){
prec= prec || 8;
while(prec%4)++prec;
var i= Math.floor(this),
s= i? i +'-':'',
num= this-i, div= 1/prec, mod= prec/4,
mag= Math.pow(10, Math.log(prec*2)/Math.log(2)),
f1= Math.round(mag*(num/div))/mag,
f= Math.round(f1);
if(f1-f&gt; div/4) f= Math.ceil(f1);
if(f== 0) return i;
if(f== prec) return i+1;
if(ret=== 1) return s+f+'/'+prec;
if(f*2== prec) return s+'1/2';
while(mod&gt;= 2){
if(f%mod=== 0) return s+ f/mod+'/'+prec/mod;
mod/= 2;
}
return s+f+'/'+prec;
}
function padLeft(str, width) {
while (str.length &lt; width) { str = "0" + str; }
return str;
}
function calculate(){
var F=document.frmRSC,

l= Number(F.txtlength.value);
w= Number(F.txtwidth.value);
d= Number(F.txtdepth.value);

Q = Number(F.txtqty.value) /* Quantity */

Lrounded = (Math.ceil(l/.125)/8).toFixed(2);
Wrounded = (Math.ceil(w/.125)/8).toFixed(2);
Drounded = (Math.ceil(d/.125)/8).toFixed(2)

F.product_number.value = 'RSC_'+padLeft(Lrounded,5)
+ '_'+padLeft(Wrounded,5)+ '_'+padLeft(Drounded,5);

F.product_id.value = '1' + (Lrounded.toFixed(1)*10)
+ '' + Wrounded.toFixed(1)*10 + '' + Drounded.toFixed(1)*10

}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name= "frmRSC"&gt;
&lt;table width="100%" border="1"&gt;
&lt;td&gt;

&lt;div class="ccms_form_element cfdiv_text" id="txtlength_container_div"&gt;
&lt;label for="txtlength"&gt;txtlength&lt;/label&gt;
&lt;input id="txtlength" maxlength="150" size="30" class=" validate['required','number']"
title="" label_over="0" hide_label="0" type="text" value="" name="txtlength" /&gt;
&lt;div class="clear"&gt;&lt;/div&gt;
&lt;div id="error-message-txtlength"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div class="ccms_form_element cfdiv_text" id="txtwidth_container_div"&gt;
&lt;label for="txtwidth"&gt;txtwidth&lt;/label&gt;
&lt;input id="txtwidth" maxlength="150" size="30"
class=" validate['required','number']" title="" label_over="0"
hide_label="0" type="text" value="" name="txtwidth" /&gt;
&lt;div class="clear"&gt;&lt;/div&gt;
&lt;div id="error-message-txtwidth"&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;div class="ccms_form_element cfdiv_text" id="txtdepth_container_div"&gt;
&lt;label for="txtdepth"&gt;txtdepth&lt;/label&gt;
&lt;input id="txtdepth" maxlength="150" size="30"
class=" validate['required','number']" title="" label_over="0"
hide_label="0" type="text" value="" name="txtdepth" /&gt;
&lt;div class="clear"&gt;&lt;/div&gt;
&lt;div id="error-message-txtdepth"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;
&lt;/table&gt;

&lt;table width="100%" border="1"&gt;
&lt;td&gt;
&lt;p&gt;product_number &lt;input type= "text" name= product_number size= 25 value= ""&gt;&lt;/p&gt;
&lt;br&gt;&lt;br&gt;

&lt;input type= "hidden" name= category_id size= 25 value= ""&gt;
&lt;p&gt;product_id &lt;input type= "text" name= product_id size= 25 value= ""&gt;&lt;/p&gt;
&lt;br&gt;&lt;br&gt;
&lt;input type= "hidden" name= product_full_image size= 25 value= ""&gt;
Quantity: &lt;INPUT TYPE = "Text" NAME = txtqty SIZE = 5 value =""&gt;
&lt;br&gt;&lt;br&gt;
&lt;p&gt;&lt;input type= "button" name= "b1" value= "Convert" onClick= "calculate()"&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@cre8tivemediaauthorNov 08.2011 — Thanks again JMRKER! Not the first time you've helped me solve an issue ?
Copy linkTweet thisAlerts:
@JMRKERNov 08.2011 — You're most welcome.

Happy to help.

Good Luck!

?
×

Success!

Help @cre8tivemedia spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...