I'm trying to validate a username and domain together (basically to validate the entire email address). I want it to be validated when I click outside either of the two inputs. I have an input tag for the username and a select tag for the possible domains.
I've looked at a number of StackOverflow posts including this one, which seemed to be exactly what I needed. I tried modifying the fiddle in the accepted answer to have as few differences as possible, but I was not able to trigger validation of my inputs. I also saw this link and made sure to not ignore hidden fields.
Here's a fiddle showing the issue, and also the HTML / JS:
<form id="form2">
<div class="usernameAndDomain">
<input type="hidden" name="usernameAndDomainValidation" class="required">
<input data-index="" id="userName" name="userName" class="userName required" type="text" maxlength="100">
<select class="domainForUser userDomain" name="userDomain">
<option value="address1.com">@address1.com</option>
<option value="address2.com">@address2.com</option>
</select>
</div>
<input type="submit" />
</form>
<a href="http://docs.jquery.com/Plugins/Validation" target="_blank">Validation Documentation</a>
$.validator.setDefaults({
ignore: [],
// any other default options and/or rules
});
$(document).ready(function () {
$.validator.addMethod('usernameAndDomainCheck', function(value, element, params) {
/* var field_1 = $('input[name="' + params[0] + '"]').val(),
field_2 = $('input[name="' + params[1] + '"]').val();
return parseInt(value) === parseInt(field_1) + parseInt(field_2); */
return false;
}, "Invalid form");
$("#form2").validate({
onkeyup: false,
onclick: false,
focusCleanup: true,
focusInvalid: false,
rules: {
usernameAndDomainValidation: {
usernameAndDomainCheck: ['userName', 'userDomain']
},
},
submitHandler: function (form) { // for demo
alert('valid form');
return false;
}
});
});
I expect that the "Invalid form" message would show since the validator method always returns false, but it is not showing that. I'm not sure what's causing that validator method to not be run.