Solved

HTML Format

Posted on 2011-03-03
5
499 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

911 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

24 Experts available now in Live!

Get 1:1 Help Now