Solved

HTML Format

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

773 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