Reputation: 65
I want to make the "other" field required ONLY if the selected text in the "select1" field is "other" the rule I'm trying is: other: { required: function(element){ return $("#select1 option:selected").text() == "Other"; } }
Am i doing something wrong? Here's the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery- validation/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$('form:first').validate({
rules: {
cname: {
required: true,
},
select1: { valueNotEquals: "0" }
},
other: {
required: function(element){
return $("#select1 option:selected").text() == "Other";
}
},
messages: {
cname: "Please enter a valid naaaammmeeee.",
select1: "Please choose a valid option",
other: "Please enter a valid other value",
},
});
$.validator.addMethod("valueNotEquals", function(value, element, arg){
return arg != value;
}, "Value must not equal arg.");
});
</script>
</head>
<body>
<form class="cmxform" id="commentForm" method="get" action="">
<fieldset>
<legend>A simple comment form with submit validation and default messages</legend>
<p>
<label for="cname">Name</label>
<em>*</em><input id="cname" name="cname"/>
</p>
<p>
<select name="select1" id="select1">
<option value="0">Please choose a value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">Other</option>
</select>
</p>
<p>
<label for="other">Other</label>
<em>*</em><input id="other" name="other"/>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</fieldset>
</form>
</body>
</html>
Upvotes: 2
Views: 8277
Reputation: 74738
updated the answer find a demo at fiddle here: http://jsfiddle.net/3jrTM/
$('form:first').validate({
rules: {
cname: {
required: true
},
select1: {
valueNotEquals: "0"
},
other: {
required: function(element) {
return $("#select1 option:selected").text() == "Other";
}
},
},
messages: {
cname: "Please enter a valid naaaammmeeee.",
select1: "Please choose a valid option",
other: "Please enter a valid other value"
}
});
$.validator.addMethod("valueNotEquals", function(value, element, arg) {
return arg != value;
}, "Value must not equal arg.");
Upvotes: 1
Reputation: 12020
There were multiple problems mostly related to JSON formatting:
one extra closing bracket
here:
select1: { valueNotEquals: "0" }
},
One closing bracket
missing here at the end of the rules
definition and just before the start of messages
definition
return $("#select1 option:selected").text() == "Other";
}
}
},
messages: {
Extra comma
at end of each options were un-necessary
Space in the validate.js path (this might be a typo here in the question itslef)
here is the corrected code:
...
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$('form:first').validate({
rules: {
cname: { required: true },
select1: { valueNotEquals: "0" },
other: { required: function(element){
return $("#select1 option:selected").text() == "Other";
}
}
},
messages: {
cname: "Please enter a valid naaaammmeeee.",
select1: "Please choose a valid option",
other: { required: "Please enter a valid other value"}
}
});
...
Upvotes: 1
Reputation: 2767
$('select').change(function() {
var text = $('select option:selected').text();
if(text == 'Other') {
$('input[name=other]').attr('disabled', false);
}
});
is this what you mean
Upvotes: 0
Reputation: 1312
You can delete the value of your option which contains the "0".
<option>Select here someting</option>
When a option contains no value atrribute, than jQuery validate handles this as empty or not given.
<select class="required"><option>Select here someting</option></select>
You can add 'required' as class to your selectbox so jQuery validate will promt a "required" warning.
Upvotes: 0