Solved

HTML Format

Posted on 2011-03-03
5
419 Views
Last Modified: 2012-05-11
I have the following code in my online form:

<b>Order Number:</b> <br />
<input type="text" name="order_no" size="30" /><br />

Is it possible for me to add some code or something to make text that is enetred into this field turn into the format below when the user tabs away from teh field?

The format I need it X:XX-XX-XXX-XXX

For instance, if the user typed in 209cr231 then tabbed away to the next field, it would automatically display 2:09-CR-023-001
0
Comment
Question by:wantabe2
  • 3
5 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 35030458
You could use http://www.w3schools.com/jsref/jsref_replace.asp with the onblur switch, will take a look at a regex
0
 
LVL 13

Expert Comment

by:darren-w-
ID: 35030640
This is very crude, what will the rule be for splitting the number? Perhaps you would be better off having a number of input fields next to each other that will accept 1 number, two strings, 3 numbers,3 numbers

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>title</title>
      <!-- // Stylesheets // -->


      <!-- // Javascripts // -->

      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

      <script type="text/javascript">
         function update(c){
            var temp = c.value.toUpperCase().split("");
            var newstring="";
            newstring+=temp[0];
            newstring+=":";
            newstring+=temp[1];
            newstring+=temp[2];
            newstring+="-";
            newstring+=temp[3];
            newstring+=temp[4];
            newstring+="-";
            newstring+="0";
            newstring+=temp[5];
            newstring+=temp[6];           
            newstring+="-";
            newstring+="0";
            newstring+="0";
            newstring+=temp[7];
            c.value = newstring;
         }

      </script>

   </head>
   <body >

      <b>Order Number:</b> <br />
      <input type="text" name="order_no" size="30" value ="209cr231" onblur="update(this);" /><br />

   </body>
</html>

Open in new window

0
 

Expert Comment

by:tobyhutton1234
ID: 35030806
Most programming languages will have a replace function check w3 schools for more information.
0
 
LVL 15

Author Comment

by:wantabe2
ID: 35030972
darren-w-,
The code you gave me works great if I type in 209cr231. The problem is, the data in this field will always be in the format 2:09-CR-023-001

So if a user would type in 209cr231 your code will work but if thee user types in 209cr23001 or 209cr0231 it does not display correctly.

Is there any way to just assign an input mask to this particular field like we can in MIcrosoft Access or Excel?
0
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 35031412
There is no way a system will be able to work out how this should be formatted with the last numbers
as this code could be read in two ways:

ie :

2:09:cr-023-001

or

2:09cr-002-031

another option:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>title</title>
      <!-- // Stylesheets // -->


      <!-- // Javascripts // -->

      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

      <script type="text/javascript">
         $(function(){
            $("input").blur(function(){
               var tmp = "";
               tmp+=$("input[name='order_no']").val();
               tmp+=":"+pad($("input[name='order_no1']").val(),2);
               tmp+="-"+pad($("input[name='order_no2']").val().toUpperCase(),2);
               tmp+="-"+pad($("input[name='order_no3']").val(),3);
               tmp+="-"+pad($("input[name='order_no4']").val(),3);
               $("#fill").html(tmp);
            });
            function pad(aBit,width){
               var l =  aBit.length+1;
               var d = width-l;
               for(i=0;i<=d;i++){
                  aBit = "0"+aBit;
               }            
               return aBit;
            }
         });

      </script>
   </head>
   <body >
      <b>Order Number: (format:X:XX-XX-XXX-XXX)</b> <br />
      <input type="text" name="order_no" maxlength="1"  size ="1"/>
      <input type="text" name="order_no1" maxlength="2" size ="2" />
      <input type="text" name="order_no2" maxlength="2" size ="2" />
      <input type="text" name="order_no3" maxlength="3" size ="3" />
      <input type="text" name="order_no4" maxlength="3" size ="3"/>
      <br />
      <div id="fill"></div>
   </body>
</html>

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now