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");
}
}
First of all, your Regexen are madness. Use the case-insensitive flag
/iand use Regex literals so you don’t have to escape everything twice:Second, you’ll want find the patterns in the English language and reuse them:
The “first” and “second” parts are repetitive, so you only need to define
firstthroughninthand their values once. The same goes fortwenty,thirtyandhundred,thousandetc. So you can break down a number likeone-hundred-twenty-firstinto “(one) hundred”, “twenty” and “first” and add the values. “eleven” through “nineteen” are exceptions you’ll need to recognize separately.Adding a
st,nd,rdorthto the end of the number is trivial by looking at its last digit.I can’t and won’t give you a complete solution here, try to get somewhere with this.