最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to validate an input type="text" element to only accept certain content? - Stack Overflow

programmeradmin0浏览0评论

I have a text input field:

<input type="text" name="email" size="25" placeholder="Email Address" required/>

Is there a way to format the input field to only allow certain text to be valid?

For example: In that particular text field, the user must input an email address which ends in @hotmail.co.uk. Any other email domains such as @yahoo will not be valid and therefore will show an error when clicking submit (Will not register the user).

I have a text input field:

<input type="text" name="email" size="25" placeholder="Email Address" required/>

Is there a way to format the input field to only allow certain text to be valid?

For example: In that particular text field, the user must input an email address which ends in @hotmail.co.uk. Any other email domains such as @yahoo. will not be valid and therefore will show an error when clicking submit (Will not register the user).

Share Improve this question edited Feb 22, 2017 at 15:43 Josh Crozier 242k56 gold badges400 silver badges313 bronze badges asked Dec 25, 2015 at 22:09 FreddyFreddy 8675 gold badges56 silver badges148 bronze badges 1
  • masked input jquery plugin for example... – Deep Commented Dec 25, 2015 at 22:11
Add a ment  | 

3 Answers 3

Reset to default 6

You can use the HTML5 pattern attribute to validate whether the string ends with @hotmail.co.uk.

In this case you can use the following regular expression:

^.*@hotmail\.co\.uk$

<form>
  <input type="text" name="email" pattern="^.*@hotmail\.co\.uk$" size="25" placeholder="Email Address" required/>
  <input type="submit" />
</form>

Alternatively, you could also just attach an input event listener to the element and check manually. This is just a basic example, feel free to customize to your needs. I would still suggest validating that the value is a valid email address by using the regex from this question.

$('input[name="email"]').on('input', function(e) {
  var isValid = this.value.match(/^.*@hotmail\.co\.uk$/) !== null;
  $(this).toggleClass('isValid', isValid);
});
.isValid {
  background: green;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" name="email" size="25" placeholder="Email Address" required/>
  <input type="submit" />
</form>

many solutions are available:

  • validate form with javascript.

  • use 2 inputs: a text input for the username and a select options or radio buttons for "@site."

  • pattern attribute on input

    Extended answer using the "pattern" attribute (from Josh Crozier):

    After the user changed the value of the input you can check it for a valid input:

    In addition you could test the input while the user is typing the value and highlight the border:

    function check(el) {
      if (new RegExp(el.pattern).test(el.value) == false) {
        alert("Bad Input")
      }
    }
    
    function test(el) {
      if (new RegExp(el.pattern).test(el.value) == false) {
        el.classList.add("bad")
      } else {
        el.classList.remove("bad")
      }
    }
    .bad {
      border-color: red;
    }
    <input pattern="^.*@hotmail\.co\.uk$" onchange="check(this)" oninput="test(this)" type="text" name="email" size="25" placeholder="Email Address" required/>

  • 发布评论

    评论列表(0)

    1. 暂无评论