将焦点设置在未定义ID的输入文本上

时间:2017-11-13 05:43:49

标签: javascript jquery focus

如果提示提示消息,我想每次在文本框上设置焦点。我该如何处理多次使用该javascript函数的地方。这是我的js代码

function validateLandline(landfield) {
        var reg = /\d{5}([- ]*)\d{6}/;            
        if (reg.test(landfield.value) == false) {
            jAlert('Kindly enter valid landline no', 'INFORMATION');
            return false;
            landfield.focus();
        }
        return true;
    }

和textbox html

<input type="text" id="txtStoreSiteL1" onchange="validateLandline(this);" maxlength="20" />

1 个答案:

答案 0 :(得分:1)

Based on the comments, I have created a small snippet below:

function validateEmail($email) {
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  return emailReg.test($email);
}

$('input').on('input focusout', function() {
  $(this).removeClass('error');
  if (!validateEmail($(this).val())) {
    $(this).addClass('error').focus();
  }
});
.error {
  border: 2px solid #d00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txtStoreSiteL1" maxlength="20" />
<input type="text" id="txtStoreSiteL2" maxlength="20" />