Form Field Input Number Automatically Convert To 3-digit By Adding Zero's In Front July 25, 2024 Post a Comment I am trying to make a form where users can input a pagenumber and jump to a certain page which is identified by its div ID. Solution 1: You could create a function for it like this:function padNum(num,length) { length = length || 3;num+=""; while(num.length < length) num="0"+num; return num; } CopyExamples:var newid = padNum(3);//003var newid = padNum(3,4);//0003CopyShorter (but less legible) function if wanted function padNum(num,length) { return Array((length||3)-((num+"").length-1)).join("0")+num; } CopyNote: functions are defaulting the length parameter to 3 when not specifiedSolution 2: used this code :Baca JugaIterate Each Div Attribute Using Jquery?Get Page To Slide Right Or Left Depending On Where The User ClickedNew Recaptcha Doesn't Work Twice In (basically) The Same Pagefunctionpad(str, max) { return str.length < max ? pad("0" + str, max) : str; } CopyOutput:pad("12", 3); // => "012"<formid="gotopage"class="uniForm"><inputid="pagenumber"name="pagenumber"value=""size="10"type="text" /></form><script> $(document).ready(function() { $('#gotopage').submit( function() { goUrl = 'index.html#pg-' + pad($('#pagenumber').val().toLowerCase(),3); window.location = goUrl; returnfalse; // Prevent the default form behaviour }); }); </script>CopySolution 3: var num = $('#pagenumber').val().toLowerCase(); varlen = num.length; var result = [0, 0].slice(0, len < 4 ? 3 - len : 0).join('') + num; CopyFiddle Share You may like these postsWhy My Input's Value Get Cleared When I Append A Child?How Can I Use Input Type Date To Dynamically Only Allow For One Year From Current Date?Change Behaviour Of Enter Key In A Phone - Angular 5Dynamically Created Select Input Using Javascript, Not Showing Dropdown Post a Comment for "Form Field Input Number Automatically Convert To 3-digit By Adding Zero's In Front"
Post a Comment for "Form Field Input Number Automatically Convert To 3-digit By Adding Zero's In Front"