Jump to content

JavaScript code to convert phone number phrase to actual phone number


Recommended Posts

I'm required to- ask the user for the phone number phrase using a standard HTML form and followed by a- java script function to convert the number phrase into actual number- the final output must show up without using html input controlsBeen trying to accomplish this using if else statements within the <script> tag but did not succeed.Any help would be greatly appreciated.Thanks

Link to post
Share on other sites

The following is an example of what immediately came to mind in trying to accomplish what you're asking. I'm sure there are other ways as well and you don't have to use a form either. Hopefully it will give you an idea that sets you in the right direction. :)

<!doctype html><html><head><meta charset="UTF-8"><title>Phone Number Phrase</title>	<script type="text/javascript">		function checkPhrase(form)		{			var number = '';			var phrase = form.phrase.value;						for(var i = 0; i < phrase.length; i++)			{							switch(phrase[i])				{					case 'a':					case 'b':					case 'c':						number = number + '2';					break;										case 'd':					case 'e':					case 'f':						number = number + '3';					break;										case 'g':					case 'h':					case 'i':						number = number + '4';					break;										case 'j':					case 'k':					case 'l':						number = number + '5';					break;										case 'm':					case 'n':					case 'o':						number = number + '6';					break;										case 'p':					case 'q':					case 'r':					case 's':						number = number + '7';					break;										case 't':					case 'u':					case 'v':						number = number + '8';					break;										case 'w':					case 'x':					case 'y':					case 'z':						number = number + '9';					break;									}			}						alert(number);			return false;					}	</script></head><body><form method="post" action="#" onSubmit="return checkPhrase(this)"><label>Enter phrase number:</label><input type="text" name="phrase" size="10"/><br/><input type="submit" name="submit" /></form></body></html>
Edited by Don E
  • Like 1
Link to post
Share on other sites

Thanks that worked!

 

Also, I would need to have the text in the button to change depending on the input in the text area. If I say "Hello World" and if the button says Submit is ....., it should display "Submit is Hello World".

I used the following code <button type="text" onclick="checkPhrase(form)"> code to accomplish but it did not happen

 

Also i would need the final output on the same page in the form area and not as a separate window. I used "document.write" instead of alert but the output was showing up in a new page.

Link to post
Share on other sites

Here's updated code based on what you mentioned. I commented out certain parts from the previous code.

<!doctype html><html><head><meta charset="UTF-8"><title>Phone Number Phrase</title>	<script type="text/javascript">		function checkPhrase(/*form*/)		{			var number = '';			//var phrase = form.phrase.value;						var phrase = document.getElementById('numPhrase').value;						for(var i = 0; i < phrase.length; i++)			{							switch(phrase[i])				{					case 'a':					case 'b':					case 'c':						number = number + '2';					break;										case 'd':					case 'e':					case 'f':						number = number + '3';					break;										case 'g':					case 'h':					case 'i':						number = number + '4';					break;										case 'j':					case 'k':					case 'l':						number = number + '5';					break;										case 'm':					case 'n':					case 'o':						number = number + '6';					break;										case 'p':					case 'q':					case 'r':					case 's':						number = number + '7';					break;										case 't':					case 'u':					case 'v':						number = number + '8';					break;										case 'w':					case 'x':					case 'y':					case 'z':						number = number + '9';					break;									}			}						document.getElementById('numPhrBut').innerHTML = "Submit is: " + phrase;			document.getElementById('dispNum').innerHTML = "Phrase converted is: " + number;			//alert(number);			//return false;					}	</script></head><body><label>Enter phrase number:</label><input id="numPhrase" type="text" name="phrase" size="10"/><br/><button id="numPhrBut" type="button" onClick="checkPhrase()">Submit</button><br/><span id="dispNum"></span><!--<form method="post" action="#" onSubmit="return checkPhrase(this)"><label>Enter phrase number:</label><input type="text" name="phrase" size="10"/><br/><input type="submit" name="submit" /></form> --></body></html>
Link to post
Share on other sites

 

The following is an example of what immediately came to mind in trying to accomplish what you're asking. I'm sure there are other ways as well and you don't have to use a form either. Hopefully it will give you an idea that sets you in the right direction. :)

<!doctype html><html><head><meta charset="UTF-8"><title>Phone Number Phrase</title>	<script type="text/javascript">		function checkPhrase(form)		{			var number = '';			var phrase = form.phrase.value;						for(var i = 0; i < phrase.length; i++)			{							switch(phrase[i])				{					case 'a':					case 'b':					case 'c':						number = number + '2';					break;										case 'd':					case 'e':					case 'f':						number = number + '3';					break;										case 'g':					case 'h':					case 'i':						number = number + '4';					break;										case 'j':					case 'k':					case 'l':						number = number + '5';					break;										case 'm':					case 'n':					case 'o':						number = number + '6';					break;										case 'p':					case 'q':					case 'r':					case 's':						number = number + '7';					break;										case 't':					case 'u':					case 'v':						number = number + '8';					break;										case 'w':					case 'x':					case 'y':					case 'z':						number = number + '9';					break;									}			}						alert(number);			return false;					}	</script></head><body><form method="post" action="#" onSubmit="return checkPhrase(this)"><label>Enter phrase number:</label><input type="text" name="phrase" size="10"/><br/><input type="submit" name="submit" /></form></body></html>

That was very elaborate and helpful! Thank you for taking the time...

Link to post
Share on other sites

So then I'm curious -- will Siri interface to Javascript?

in what way? Javascript run in a browser typically, Siri runs on the iOS platform itself.

Link to post
Share on other sites

Ah, I getcha. Not sure, usually Sire responds to prompts and keywords. Not sure if it could handle filling out an online form on a webpage.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...