Solved

HTML Format

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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