There was a small typo in validateLastNameInput. Here's the corrected
code.

<input id="firstName" class="first-name-input name-input"/>
<input id="lastName" class="last-name input name-input"/>
jQuery.validator.addMethod('first-name-input', validateFirstNameInput,
'First name is missing.');
jQuery.validator.addMethod('last-name-input', validateLastNameInput,
'Last name is missing.');
jQuery.validator.addMethod('name-input', validateNameInputs, 'First
and Last name are missing.');
var firstNameInput = document.getElementById('firstNameInput');
var lastNameInput = document.getElementById('lastNameInput');
function validateFirstNameInput(value, element) { return !
lastNameInput.value || value; }           // 1
function validateLastNameInput(value, element) { return !
firstNameInput.value || value; }            // 2
function validateNameInputs(value, element) { return
firstNameInput.value && lastNameInput.value; } // 3



On Jan 30, 11:18 am, Mihai Danila <virid...@gmail.com> wrote:
> I think you can put three validators on the fields, with
> jQuery.validator.addMethod. I don't know if you can add multiple
> validators for the same CSS class, so I'm putting extra CSS classes on
> the inputs:
> <input id="firstName" class="first-name-input name-input"/>
> <input id="lastName" class="last-name input name-input"/>
>
> jQuery.validator.addMethod('first-name-input', validateFirstNameInput,
> 'First name is missing.');
> jQuery.validator.addMethod('last-name-input', validateLastNameInput,
> 'Last name is missing.');
> jQuery.validator.addMethod('name-input', validateNameInputs, 'First
> and Last name are missing.');
>
> var firstNameInput = document.getElementById('firstNameInput');
> var lastNameInput = document.getElementById('lastNameInput');
> function validateFirstNameInput(value, element) { return !
> lastNameInput.value || value; }           // 1
> function validateLastNameInput(value, element) { return
> firstNameInput.value || value; }            // 2
> function validateNameInputs(value, element) { return
> firstNameInput.value && lastNameInput.value; } // 3
>
> Validator #1 will always run for the first name input, but will choose
> to never display a message as long as the last name input is empty. If
> the last name input is not empty, then this validator will actually
> check the given value (the value of the first name input) and validate
> accordingly.
>
> Validator #2 is in a similar situation.
>
> Validator #3 will display an error only if both are empty.
>
> Think of them as working together to only activate the errors when
> only a subset of the possible conditions get applied.
>
> I'm looking for a way to do this with a single validator myself. I
> have dynamic forms and more complex situations, so I can't employ this
> solution. If anyone knows how to do it, please don't hesitate to put
> it in.
>
> Mihai
>
> On Jan 5, 4:26 pm, claudes <claudina.sar...@gmail.com> wrote:
>
> > i have a group that contains both first and last name. i'm wondering if it 
> > is
> > possible to have custom messages for groups. i'm trying to achieve something
> > to the following:
>
> > a. if both first and last name are not filled in: First and Last name are
> > Required
> > b. if only first is filled in: last name is required
> > c. if only last is filled in: first name is required
>
> > is this possible?
> > --
> > View this message in 
> > context:http://www.nabble.com/validation-plugin%3A-how-to-add-custom-messages...
> > Sent from the jQuery General Discussion mailing list archive at Nabble.com.

Reply via email to