0

I have an app that is written on the top of ASP.NET Core 3. My project use jQuery.Validation and jQuery.Validation-Unobtrusive

I have a form that required either Phone or Email fields. My form is also using the input-mask to force the user to type in a valid US-phone number or email.

Here is how I configured my form-validation

$('#ScheduleTourForm').validate({
    ignore: "",
    debug: true,
    rules: {
        Day: {
            required: true,  // require date
            date: true,      // value must be a datetime value
        },
        Time: {
            required: true,  // require time
            date: true,      // value must be a datetime value
        },
        Phone: {
            required: '#Email:blank',  // Only require Phone if Email is blank
            phoneUS: true              // US-phone format is required
        },
        Email: {
            required: function (e) {
                var p = $("#Phone");
                return p.is(':empty') || p.val() === '(___) ___-____'; // Only require email when Phone is empty or has the default mask.
            },
            email: true // must be a valid email
        }
    },
    submitHandler: function (form) {
        console.log('submit form...');

        //$(form).ajaxSubmit();
    }

});

However, that does not seems to be requiring nither the Phone or the Email. An example of the code not working can be found on this jsfiddle

How can I correctly require either Phone or Email?

John
  • 245
  • 3
  • 8
  • You cannot use the Unobtrusive Validation plugin AND initialize the plugin with a your own call to `.validate()`. The whole point of the Unobtrusive plugin is that it picks up your inline data attributes and constructs the `.validate()` method for you. Since the validation plugin ignores duplicate calls to this method, it's ignoring your call to `.validate()`. Secondly, there is a rule called `require_from_group` that does what you want and it's part of the `additional-methods.js` file. – Sparky Apr 07 '20 at 02:53
  • Is it possible to use unobtrusive to ensure at least one field is required? – John Apr 07 '20 at 05:47
  • AFAIK, `require_from_group` cannot be implemented or configured via data attributes, so if you're using Unobtrusive, I don't see how. – Sparky Apr 08 '20 at 16:16

0 Answers0