-
-
Save ASDAFF/3f308fc4dc0e110f486d2fe9b667f5d5 to your computer and use it in GitHub Desktop.
Настройка jquery validate/ одно из двух полей + мобильный телефон (Россия)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(document).ready(function () { | |
| var phoneInput = $('#cnPhone'); | |
| phoneInput.mask('+7(000)-000-0000'); | |
| phoneInput.on('focus', function () { | |
| $(this).val('+7('); | |
| }); | |
| jQuery.validator.addMethod('mobileRu', function () { | |
| var phRe = /^\+7\(([0-9]{3})\)\-([0-9]{3})\-([0-9]{4})$/; | |
| return phoneInput.val().match(phRe); | |
| }, 'Введите корректный номер телефона'); | |
| jQuery.validator.addMethod("messages", function(value, element) { | |
| return this.optional(element) || /^[a-z0-9\-\s]+$/i.test(value); | |
| }, "Сообщение должно содержать только буквы, цифры или пробелы"); | |
| jQuery.extend(jQuery.validator.messages, { | |
| maxlength: jQuery.validator.format("Поле доле должно содержать не более {0} символов."), | |
| minlength: jQuery.validator.format("Поле должно содержать как минимум {0} символов."), | |
| required: "Это поле обязательно для заполнения", | |
| email: "Пожалуйста, введите корректный e-mail адрес", | |
| lettersonly: "Поле должно содержать только буквы" | |
| }); | |
| $(document).on('blur', '#contactForm input[type=text], #contactForm input[type=tel], #contactForm input[type=email]', function () { | |
| if ($(this).hasClass('valid')) return; | |
| $(this).val(''); | |
| }); | |
| var form = $('#contactForm'); | |
| form.validate({ | |
| rules : { | |
| cnPhone: { | |
| required: function(element) { | |
| var result = false; | |
| phoneInput.removeClass('mobileRu'); | |
| if ($("#cnEmail").val() == '') { | |
| result = true; | |
| if (!phoneInput.hasClass('mobileRu')) { | |
| phoneInput.addClass('mobileRu'); | |
| } | |
| } | |
| return result; | |
| } | |
| }, | |
| cnEmail: { | |
| required: function(element) { | |
| var result = false; | |
| if ($("#cnPhone").val() == '') { | |
| result = true; | |
| } | |
| return result; | |
| } | |
| } | |
| }, | |
| messages: { | |
| cnPhone : { | |
| required : 'Введите одно из полей: ваш телефон или e-mail' | |
| }, | |
| cnEmail : { | |
| required : 'Введите одно из полей: ваш e-mail или телефон' | |
| } | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment