SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Run Validation if

    Hi everyone,

    I am trying to run the following script to validate if textbox 'vSurveyAnswer[1].answer' is empty ONLY if the 'groupid' equals 542. I can't seem to get this to work, it validates regardless. Any help would be great. I also need to be able to validate other text boxes when the groupID is a different number (groupID comes from a querystring parameter)

    Code:
    $(document).ready(function(){
      $('#continueResBtn').click(function() {
      if($("#groupid").val() === "542"){
    	if ($("input[name='vSurveyAnswer[1].answer']").val().length === 0){
          alert('Please Enter Start Date');
           return(false);
      }
      else return(true)
    }
    });
    });
    Thanks!!

  2. #2
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    If your #groupid is a plain input field then the above code should work fine.

    This code in isolation:
    http://jsfiddle.net/GeekyJohn/CX6Ba/

    Seems to work just fine.

    Do you perhaps have a sample page online somewhere to look at?
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks AussieJohn looks like it is working on JSFiddle, I made a couple changes to my local and it seems to be working now.

    Quick question, I would like to include the ability to make the groupID variable, example: if groupid = 542 or 543 or 544 and vSurveyAnswer is blank then fire the alert, do you know of a quick way of doing this?

    Thanks for your help!!!

  4. #4
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    If your groupId comes from a query string, it is pretty trivial to retrieve it with a simple JavaScript helper function https://snipt.net/geekyjohn/get-url-param/

    You could then call getUrlParam("groupId") and it would return the value of the parameter (or undefined)
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  5. #5
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The purpose is that not all groupid values will be used to validate against, I have specify which ones. I am actually pulling the querystring param from the URL via a jSP call ont eh page and placing it into a hidden field. I am then using the validation script to check the groupID which validates the survey fields. Does that make sense? I am not a big JS guy so I am not sure if I can use the link you supplied.

  6. #6
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Makes sense. If you have a list of the group IDs that would require the validation you could put them in an array,
    e.g.

    Code:
    var groupIDs = [1, 2, 3, 4, 234, 542];
    You could then perform your validation if the current group ID falls is found in that array (using jQuery's inArray method)

    Code:
    if ( $.inArray(groupID, groupIDs) ) { 
      //perform validation
    }
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  7. #7
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi AussieJohn, not sure what's happening, but it doesn't seem to work. Here is an example...
    http://jsfiddle.net/CX6Ba/12/

  8. #8
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Whoops yes, the .inArray() function returns an index or -1 if not found (and not a boolean).

    I've updated the fiddle: http://jsfiddle.net/CX6Ba/21/

    Basically made inArray check for equality against "-1" and making sure that the group ID is actually retrieved from the field and parsed as a number.
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  9. #9
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the ticket, thanks AussieJohn!!


Bookmarks

Posting Permissions

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