1

I am creating a web application where I need to create a form. This form has some fields like input box and text area. These two fields only need to accept only English keyboards characters.

I need to accepts only: A-Za-z0-9!@#$%^&():"';<>,.?/|*

How can I do it?

This is my form:

$(function(){
  $(".english_only").on("change", function(){
   
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" class="english_only"/>
  <textarea class="english_only"></textarea>
</form>
  • `pattern="^[A-Za-z]+$"` – Avinash Raj Jun 04 '16 at 11:25
  • sir, how can i use this? –  Jun 04 '16 at 11:29
  • @RivnatNasah a short search will with `input` and `pattern` will lead you to [`MDN: : pattern`](https://developer.mozilla.org/en/docs/Web/HTML/Element/Input#attr-pattern) – t.niese Jun 04 '16 at 11:44
  • 1
    Possible duplicate of [How to accepts only character values in HTML5](http://stackoverflow.com/questions/15411636/how-to-accepts-only-character-values-in-html5) – t.niese Jun 04 '16 at 11:44
  • I need all these `A-Za-z0-9!@#$%^&():"';<>,.?/|*` is there any shortcut for these?? –  Jun 04 '16 at 11:47
  • Then you need to look for e.g. [Regex any ascii character](http://stackoverflow.com/questions/3203190). There are so many questions targeting this topic. – t.niese Jun 04 '16 at 11:51
  • This question is a possible duplicate. You can use this solution listed. Look in the solution and put the characters you wish you accept in the string. This, of course, would be done using JavaScript. http://stackoverflow.com/questions/15411636/how-to-accepts-only-character-values-in-html5 – Dale Jun 04 '16 at 11:52

2 Answers2

1

Try this..

$(document).ready(function(){
    $(".english_only").on("keyup", function(){
      console.log(/^[ A-Za-z_0-9@./#&+-]*$/i.test($(this).val().trim()));
    });
    $('.english_only').on("paste",function(e)
    {
        e.preventDefault();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" class="english_only"/>
  <textarea class="english_only"></textarea>
</form>
Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39
  • How can i protect the user to write the false letter?? I mean i get false message but how to control? –  Jun 04 '16 at 15:41
0

/^[ A-Za-z_@./#&+-]*$/i and add more special characters which you want to allow.

$(function(){
  $(".english_only").on("change", function(){
        var str = $(this).val();
        str = /^[ A-Za-z_0-9@./#&+-]*$/i.test(str).trim();
    });
 });
pradeep1991singh
  • 8,185
  • 4
  • 21
  • 31