Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    141
    Thanks
    64
    Thanked 1 Time in 1 Post

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    141
    Thanks
    64
    Thanked 1 Time in 1 Post
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •