Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HTML Format

Posted on 2011-03-03
5
Medium Priority
?
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

721 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