1

код который делает блок с ошибками

function showError(container, errorMessage) {
      container.className = 'error';
      var msgElem = document.createElement('span');
      msgElem.className = "error-message";
      msgElem.innerHTML = errorMessage;
      container.appendChild(msgElem);
    }

код который проводит валидацию

function validate(form) {
      var validationFailed = false;
        $(".valid-form input").each(function(){
          if (!this.value) {
            showError(this.parentNode, $(this).attr('title'), 'Filed cannot be empty');
            validationFailed = true;
          } else if (this.value.indexOf('\'') >= 0 || this.value.indexOf('"') >= 0) { 
              showError(this.parentNode, $(this).atrr('title'), 'Field cannot contain quotes');
                        validationFailed = true;
            }
            else {
                resetError(this.parentNode);
            }
        });
        return !validationFailed;
    }

вот код который отвечает за проверку на скобки

else if (this.value.indexOf('\'') >= 0 || this.value.indexOf('"') >= 0) { 
              showError(this.parentNode, $(this).atrr('title'), 'Field cannot contain quotes');
                        validationFailed = true;
            }

вот хтмл форма

<form action="" name="registration" id="myForm">
    <div class="valid-form">
        <label for="firstname">Имя</label>
        <input type="text" name="firstname" title="Укажите ваше имя" placeholder="John"/>
      </div>
</form>

как сделать что бы при вводе кавычек отображался другой title в ошибку а не этот с Укажите ваше имя?

1 ответ 1

0

Добавьте третий параметр в функцию showError.

<form action="" name="registration" id="myForm">
  <div class="valid-form">
    <label for="firstname">Имя</label>
    <input type="text" name="firstname" 
      data-messageempty="Укажите ваше имя" data-messagequote="кавычка в вашем имени"/>
  </div>
</form>

function validate(form) {
  var validationFailed = false;
  $(".valid-form input").each(function(){
    if (!this.value) {
      showError(this.parentNode, $(this).data('messageempty'), 'Filed cannot be empty');
      validationFailed = true;
    } else if (this.value.indexOf('\'') >= 0 || this.value.indexOf('"') >= 0) { 
      showError(this.parentNode, $(this).data('messagequote'), 'Field cannot contain quotes');
      validationFailed = true;
    } else {
      resetError(this.parentNode);
    }
  });
  return !validationFailed;
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.