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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form instant validation - validate field value based on other field value

    I am using javascript to instantly validate as a user types into a form. Livevalidation is where I got the snippet. I like, it, but it doesn't allow to validate one field value based in another value in the form.

    So I tried to alter the code and it works great in Mozilla but not in IE. That probably means I'm doing something wrong. I'm not really familar with Javascript.

    My situation. If a user picks "Other" in drop down, then he/she needs to specify in the next field why they picked "Other"; So with my instant validation, I want it to generate an error when the user tries to leave the second field (Other) blank.

    My form with two fields and Javascript:

    Code:
    <form action="ex.php" method="post">
    
    <fieldset>
    <legend><span>Contract Details</span></legend>
    <ol>
    <li>
    <label for="contractType">Type Of Contract</label>
    <select name="contractType" id="contractType">
    
            <option>Contract 1</option>
            <option>Contract 2</option>
            <option>Other</option>
    
    </select>
    
    </li>
    <li>
    <label  for="contractTypeOther">If you picked "Other", specify</label>
    <input id="contractTypeOther" name="contractTypeOther" class="text" type="text" maxlength="100" />
    
    <script type="text/javascript">
    var contractTypeOther = new LiveValidation('contractTypeOther');
    contractTypeOther.add( Validate.Custom3, { match: 'contractType' } );
    </script>
    </li>
    </ol>
    </fieldset>
    
    <fieldset class="submit">
    <input class="submit" type="submit" value="Submit" />
    </fieldset>
    </form>
    The code that is being fetched (in my JS file):

    Code:
    Custom3: function(value, paramsObj){
          	var paramsObj = paramsObj || {};
        	var message = paramsObj.failureMessage || "You picked \"Other\". Please specify!";
        	var match = paramsObj.match.nodeName ? paramsObj.match : document.getElementById(paramsObj.match);
        	if(match.value =='Other' && value==''){
        	  	Validate.fail(message);
        	}
        	return true;
        },
    It does work in Firefox, not in IE. Please help me catch the bad guy!

  • #2
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know this was posted a few years ago but I'm trying to do the same thing, validate a field based off of another using LiveValidation. I want to use your code but like you said, it doesn't work in IE. Did you find a solution to this?

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,166
    Thanks
    23
    Thanked 600 Times in 599 Posts
    Since you did not post your code I'll use the first example.
    I'd a made the "Why did you choose other" input box invisible and only made it visible if OTHER were chosen.
    In the javascript I would have used an if statement to evaluate that box if the value of 'contractType' was other.

    Please note the above code for the select is not coded correctly, look it up.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Tags for this Thread

    Posting Permissions

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