...

View Full Version : Using jquery to check textboxes



kreut
02-24-2011, 12:59 PM
Hello,

I'd like to write some jquery code which will check to see if the contents of a textbox are equal to some value after the user leaves the textbox. they should get a message saying Correct or Incorrect . As an example:


<td>-2</td>
<td><input name="ans1" type="text" size="3" maxlength="3" /></td>
</tr> < The correct answer here would be 5 >
<tr>
<td>-1</td>
<td><input name="ans1" type="text" size="3" maxlength="3" /></td> < The correct answer here would be 4>

And, I don't wish to abuse the forum, but as I'm learning javascript and jquery, I'd like to see solutions to both (I learn by example), so I'm posting the same question in the main JavaScript programming forum with a request for a Javascript solution; I hope that this isn't bad form!:confused:

Thank you.....

Spudhead
02-25-2011, 02:57 PM
I'd like to write some jquery code which will check to see if the contents of a textbox are equal to some value after the user leaves the textbox. they should get a message saying Correct or Incorrect


$('input[type=text]').blur(function(){
$('<span>').text(($(this).val()=='the_right_answer') ? 'Correct' : 'Wrongamundo').insertAfter($(this));
});


And, I don't wish to abuse the forum, but as I'm learning javascript and jquery, I'd like to see solutions to both (I learn by example), so I'm posting the same question in the main JavaScript programming forum with a request for a Javascript solution; I hope that this isn't bad form!:confused:.

Cross-posting is generally frowned upon. But as a word of advice - don't get caught thinking you can learn jQuery instead of javascript. jQuery is a javascript framework; it's great at reducing the amount of code you have to write, but over-reliance on it will get you into all sorts of pain.

kreut
02-25-2011, 05:24 PM
Thank you for both your solution (it actually makes sense!) and your suggestion about understanding javascript (I got the same advice in the other forum).

In addition, I won't do any cross postings in the future. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum