...

View Full Version : Form instant validation - validate field value based on other field value



flukyspore
09-21-2010, 10:00 PM
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:


<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):


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!

yakitori
07-17-2013, 06:04 PM
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?

sunfighter
07-18-2013, 04:13 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum