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 08-28-2012, 10:58 PM   PM User | #1
nick2price
Regular Coder

 
Join Date: May 2009
Posts: 105
Thanks: 47
Thanked 1 Time in 1 Post
nick2price is an unknown quantity at this point
Validation issue

Hi guys, having a bit of a problem validating something. To put it simply, I have a form and when you start typing into one of the fields, a load of suggestions are made (obtained from my database). Doing real time validation on what they select from these suggestions was difficult, so I done a regex instead. This is what I came up with

Code:
$("[name=selectGame]").blur(function(){
var to = $("input[name= selectGame]").val();
	
if(to != 0)
{
	if(isValidExpression(to)){
		$("#game").css({
			"background-image": "url(‘/tick.png')"
		});			
	} else {
		$("#game").css({
			"background-image": "url(' /cross.png')"
		});			
	}
}
else{ 
$("#game").css({"background-image": "none"});
}
});
So firstly it checks thats the inputs not null, and if it is, it will display nothing in the image holder "game". If there is input, it firstly checks it against the regex. If it matches, it displays a tick, and if it doesnt, it displays a cross.

This is all done in real time. At the moment, everything works fine apart from one small thing which I was hoping to fix. I get the expected outcomes barring one situation. As soon as I select something from the suggested items which pop up, I am displayed a cross until I click into another field. So it does eventually show the correct image (a tick), but only when I click away. Is there anyway to stop any image from showing until I click away from the field? Or maybe something better than blur which I can use?

Cheers
nick2price is offline   Reply With Quote
Old 08-28-2012, 11:17 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Maybe use onchange instead of onblur?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-28-2012, 11:25 PM   PM User | #3
nick2price
Regular Coder

 
Join Date: May 2009
Posts: 105
Thanks: 47
Thanked 1 Time in 1 Post
nick2price is an unknown quantity at this point
Would I be able to use that in a similar way to what I am doing now e.g.
Code:
$("[name='gameChoic']").onchange(function(){
I have attempted it, but it doesnt seem to work.
nick2price is offline   Reply With Quote
Old 08-29-2012, 12:37 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Yes, but with jQuery it's probably just "change", not "onchange". After all, in normal JS, it is "onblur" not just "blur" as you were using.

I don't use jQuery; just applying parallel logic.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant 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 12:00 PM.


Advertisement
Log in to turn off these ads.