Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 19th, 2004, 08:17 AM
Authorized User
 
Join Date: Jan 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Text box value

How do I get a text box to add leading zero's to any number entered. So the text box will be 9 characters max in length. If a user enter 5 numbers, I would the other 4 numbers to be leading zero's. For example, if a user entered 12345, I would like the result when the web form is submitted to look like 000012345.

Please help.

Thanks,
Tee
 
Old October 19th, 2004, 08:49 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Tee,

Try this...

Code:
function CheckValues(pForm){
    var ok = true;
    var temp = pForm.myText.value;
    if(/^\d*$/.test(temp)){
        var padding = "000000000";
        temp = padding.substr(temp.length) + temp;
    }else{
        alert("Please enter an integer value");
        ok = false;
        temp = "";
    }
    pForm.myText.value = temp;    
    return ok;
}
...

<form id="myForm" name="myForm" onsubmit="return CheckValues(this);" >
    <input type="text" id="myText" name="myText" />    
    <input type="submit" value="Go" />
</form>
HTH,

Chris


 
Old October 19th, 2004, 10:39 AM
Authorized User
 
Join Date: Jan 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Chris..I will give it a try.:)

 
Old October 19th, 2004, 11:13 AM
Authorized User
 
Join Date: Jan 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, that did not work.

thanks though
 
Old October 19th, 2004, 11:17 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey Tee,

Strange - works for me in IE & Mozilla, can you post your code?

Thanks,

Chris

 
Old October 19th, 2004, 01:02 PM
Authorized User
 
Join Date: Jan 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is just a simple form going to a SQL DB.

<form method="get" action="insert.asp" name="Form" >

        <div align="center">
        <table border="0" width="660" id="table1">
            <tr>
                <td>
                <p align="center"><br>
                <b><br>
&nbsp;</b></td>
                <td width="433">
                <p align="center">
                &nbsp;</td>
            </tr>
        </table>

        <table width="651" border="0">
          <tr>
            <td colspan="2">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2"><b>[u]Customer Information</u></b></td>
          </tr>

          <tr>
            <td>Account #</td>
            <td><span style="font-size: 11pt"><input type="text" name="acct" size="9"></span></td>
          </tr>
          <tr>
            <td>Phone number:<br>
            <b>(including area code)</b></td>
            <td><span style="font-size: 11pt">
            <input type="text" name="phone" size="20"></span></td>
          </tr>
          <tr>
            <td colspan="2">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2">Please select the
            equipment the customer is missing (check all that apply):</td>
          </tr>
          <tr>
            <td colspan="2">
            <table border="0" width="651" id="table2">
                <tr>
                    <td width="108">
                    <span style="font-size: 11pt">
                    <input type="checkbox" name="digital" value="yes"></span>&nbsp;
                    Yes</td>
                    <td width="108">
                    <span style="font-size: 11pt">
                    <input type="checkbox" name="HDTV" value="yes"></span>&nbsp;
                    No</td>
                    <td width="108">&nbsp;</td>
                    <td width="108">&nbsp;</td>
                    <td width="108">&nbsp;</td>
                    <td width="109">&nbsp;</td>
                </tr>
            </table>
            </td>
          </tr>
          <tr>
            <td colspan="2">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2" height="28"><p>

                <input name="Submit" type="submit" value="Submit">
                <input type="reset" name="R1" value="Reset">

              </p></td>
          </tr>
        </table>
            </div>
            <p>&nbsp;</p></td>



</form>


thanks for your help!


 
Old October 20th, 2004, 03:25 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Tee,

Add this script to your page...
Code:
function PadIntField(pField){
    var ok = true;
    var temp = pField.value;
    alert(pField.maxLength);
    if(/^\d*$/.test(temp)){
        var padding = "";
        for(var i = 0; i < pField.maxLength; i++){
            padding += "0";
        }
        temp = padding.substr(temp.length) + temp;
    }else{
        alert("Please enter an integer value");
        ok = false;
        temp = "";
    }
    pField.value = temp;    
    return ok;
}
then add
Code:
onsubmit="return PadIntField(this.acct);"
to your form tag and
Code:
maxLength="9"
to your acct input tag.

Cheers,

Chris







Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab Values From List Box into Text Box phungleon VB How-To 2 June 19th, 2008 10:33 PM
Count in combo box(display results in text box) mboyisis Access 4 April 4th, 2008 07:08 AM
Extract text from text file & put in dropdown box tsukey Beginning PHP 5 July 20th, 2004 09:49 PM
Rich Text Box vs Text Box snowy0 VB.NET 2002/2003 Basics 1 February 17th, 2004 02:11 PM
Search using drop down list box and a text box tcasp Classic ASP Basics 1 July 31st, 2003 02:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.