Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
i want a  generic  method  using  javascript to  convert

the values like first floor, second floor, ground floor will be converted to below format,

Ground Floor: Gr. Floor

First Floor: 1st Floor

Twenty floor: 20th Floor
  right now  i am  using  following code
but that not the   correct way as i cannot  write for all the floors (50th floor,90th floor) so please   help me  with this

function validpress(iKeyascii)
 {
if( iKeyascii=='77'||iKeyascii=='76'||iKeyascii=='68'||iKeyascii=='73'||iKeyascii=='88'||iKeyascii=='86'||iKeyascii=='67')
{
alert("Roman numbers are not allowed.");
return false;
}
var txt= document.getElementById('<%=TextBox1.ClientID %>').value;
//document.getElementById('<%=TextBox1.ClientID %>').value = txt.toUpperCase();
 var patternforGFloor = new RegExp('\\b[G|g][R|r][O|o][U|u][n|N][D|d]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor1Floor = new RegExp('\\b[F|f][I|i][R|r][S|s][T|t]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor2Floor = new RegExp('\\b[S|s][E|e][C|c][O|o][N|n][D|d]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');

var patternfor3Floor = new RegExp('\\b[T|t][H|h][I|i][R|r][D|d]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor4Floor = new RegExp('\\b[F|f][O|o][U|u][R|r][T|t][H|h]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');
var patternfor5Floor = new RegExp('\\b[F|f][I|i][F|f][T|t][H|h]*\\.*\\s*[F|f][L|l][O|o][O|o][R|r]\\b');


     if (txt.match(patternforGFloor) )
     {
     document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternforGFloor,"Gr.Floor");
     }

      if (txt.match(patternfor1Floor))
     {

     document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternfor1Floor,"1st. Floor");
     }

     if (txt.match(patternfor2Floor) )
     {
     document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternfor2Floor,"2nd.Floor");
     }

      if (txt.match(patternfor3Floor))
     {

     document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace(patternfor3Floor,"3rd. Floor");
     }
  if (txt.match( patternfor4Floor))
     {

     document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace( patternfor4Floor,"4th. Floor");
     }

if (txt.match( patternfor5Floor))
     {

     document.getElementById ('<%=TextBox1.ClientID %>').value=txt.replace( patternfor5Floor,"5th. Floor");
     }

 }
Posted

1 solution

There is no need to specify every upper and lower case. Also, the first part is the most important part because it tells you what floor you on. Floor itself isn't that interesting and doesn't change.

You can use two arrays to search and replace, something like:
var find_arr=["ground","first","second"];
var repl_arr=["Gr. Floor","1th. Floor","1th. Floor"];

Also, you can use the /i modifier at the end of your regex to indicate case insensitive. That way you can do without the confusing [E|e][[T|t]...

http://www.w3schools.com/jsref/jsref_obj_regexp.asp[^]

Also... I wouldn't even check specifically for roman numbers and just show an error message when the user wants to commit. Just give the people some good information about what is expected and by giving some examples it will be even more clear. The same goes for "floor". If people can only input floor numbers then there is no need to check for floor because it can be discarded. It doesn't give anymore info on what floor is entered. You could even question why people need to do the extra typing.

Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900