Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2013, 04:26 PM   PM User | #1
tonyman
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
tonyman is an unknown quantity at this point
Quick I/O questions

Hey everyone,

I only had a quick question, sorry about opening a whole new thread for this. Its probably a stupid question, but I am curious to know if there is a simple way to do what I am trying to do.

Okay, so essentially I want the user to input a number in input box 1 and 2 and I want the two boxes to cross reference each other to say if the numbers match or not.

So for example. I put the number '5' in Input Box 1. And I put the number '6' in Input Box 2. In a text area it will display if its a match or not.

I have tried to get it to work and cannot figure out a way to match them. I can get them to add together, display seperately but am puzzled with a way to get them to match. The only way I got it to work was with a realllllllly long if statement and using prompts (which is a little cluttery).

Below is simple code. I dont really need someone to add code, just let me know a method to get the inputs to cross reference each other.

Thanks!

PS Keep in mind that I have very little code. I want to know if this is possibly before I give up my prompt and if statements code to change to this.

Code:
<html>
<head>
	<script type="text/javascript">
	
	
	function one() {
		alert(i1.value);
		}
	
	
	

		
	</script>
</head>
<body>
	<input id="i1" size="1">
	<br />
	<input id="i2" size="1">
	
	<br />
	
	<button onclick="one()" id="c1" >Calculate</button>

	<br />
	
	<textarea name="t1"></textarea>
</body>
</html>
tonyman is offline   Reply With Quote
Old 02-14-2013, 04:40 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Just make a straightforward comparison, but ensure that the values entered by the user are changed from strings to numbers using Number();
Instead of ===, == would serve perfectly adequately here. To find out more, Google for Javascript comparison operators.

Code:
First Number <input type = "text" id="inp1" size="1">
<br>
Second Number <input type = "text" id="inp2" size="1">
<br>
<input type = "button" value = "Compare" onclick = "compare()">

<script type = "text/javascript">
function compare() {
var a = Number(document.getElementById("inp1").value) || 0; // Trap NaN entries
document.getElementById("inp1").value = a;  // write it back to the field
var b = Number(document.getElementById("inp2").value) || 0; // Trap NaN entries
document.getElementById("inp2").value = b;  // write it back to the field

if (a === b) {  // identical match
alert ("The two numbers match");
}
else {
alert ("The two numbers are different");  // alerts are regarded as obsolete and should only be used for testing.
}
}

</script>
Quizmaster: How many days will there be in the year 2014?
Contestant: Er... is it 60 or 52?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-14-2013 at 04:50 PM..
Philip M is offline   Reply With Quote
Old 02-14-2013, 04:47 PM   PM User | #3
snakehill
New Coder

 
Join Date: Sep 2011
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
snakehill is an unknown quantity at this point
To add to Philip, if you want to display the results in the textarea instead of an alert (that's at least what I got from your question), you could give the textarea an ID and replace the alerts with
Code:
document.getElementById('t1').value = "The two numbers match/The two numbers are different";
snakehill is offline   Reply With Quote
Old 02-14-2013, 04:48 PM   PM User | #4
tonyman
New to the CF scene

 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
tonyman is an unknown quantity at this point
Okay perfect. That worked! Thanks a million. I am going to switch all my stuff over now. This will be much cleaner then having a bajjillion prompts pop up and using a crazy amount of if statements.

Thanks again!
tonyman is offline   Reply With Quote
Old 02-14-2013, 04:54 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by tonyman View Post
Okay perfect. That worked! Thanks a million. I am going to switch all my stuff over now. This will be much cleaner then having a bajjillion prompts pop up and using a crazy amount of if statements.

Thanks again!
Prompts are regarded as obsolete and should never be used in modern Javascript coding.

It is better to use DOM methods to display a message to the user in a <span> or a <div> rather than place the message in a textarea or textbox.

A practical use for comparing two text values is to check that the user's email address and the confirm (repeat) email address values match.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-14-2013 at 04:56 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:06 AM.


Advertisement
Log in to turn off these ads.