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

How to allow only english,numeric and special chars in textbox using jquery or javascript? - Stack Overflow

programmeradmin3浏览0评论

I want to allow only English,numeric and special characters to be typed in my web page. i want to apply this thing using jquery or javascript. Actually my application is in 2 languages so for that i want to do this. I want to do the same thing with Arabic language too.. please help me. How can I do that?

I want to allow only English,numeric and special characters to be typed in my web page. i want to apply this thing using jquery or javascript. Actually my application is in 2 languages so for that i want to do this. I want to do the same thing with Arabic language too.. please help me. How can I do that?

Share Improve this question edited Oct 11, 2012 at 17:05 Ram asked Oct 11, 2012 at 16:42 RamRam 1,14111 gold badges28 silver badges52 bronze badges 3
  • 3 1- what have you tried? 2- what are "english" chars? is $ one of them? i know "english" uses it. – yodog Commented Oct 11, 2012 at 16:43
  • $('input').change(function() { $(this).val('English characters'); } – mayhewr Commented Oct 11, 2012 at 16:58
  • I have updated the question please review again............. – Ram Commented Oct 11, 2012 at 17:06
Add a ment  | 

5 Answers 5

Reset to default 5

You can try the following code that uses JavaScript replace method. The replace method accepts a regex pattern and so you can define pretty much anything you want/don't want typed in the textbox:

<script type="text/javascript">

      $('#textboxID').bind('keyup blur',function() { 
            $(this).val($(this).val().replace(/[^A-Za-z0-9]/g,''))
        });

</script>

Here's the jsFiddle to try this out: http://jsfiddle/leniel/rtE54/


After a bit more thinking I implemented this code:

$("#mytextbox").on("keypress", function(event) {

    // Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase, digits 0 to 9 and white space)
    // For more on JavaScript Regular Expressions, look here: https://developer.mozilla/en-US/docs/JavaScript/Guide/Regular_Expressions
    var englishAlphabetDigitsAndWhiteSpace = /[A-Za-z0-9 ]/g;

    // Retrieving the key from the char code passed in event.which
    // For more info on even.which, look here: http://stackoverflow./q/3050984/114029
    var key = String.fromCharCode(event.which);

    //alert(event.keyCode);

    // For the keyCodes, look here: http://stackoverflow./a/3781360/114029
    // keyCode == 8  is backspace
    // keyCode == 37 is left arrow
    // keyCode == 39 is right arrow
    // englishAlphabetDigitsAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
    if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetDigitsAndWhiteSpace.test(key)) {
        return true;
    }

    // If we got this far, just return false because a disallowed key was typed.
    return false;
});

$('#mytextbox').on("paste",function(e)
{
    e.preventDefault();
});

You can read more about it here: JavaScript regex + jQuery to allow only English chars/letters in input textbox

You can limit the keys base on the onkeypress checking if the key is in the limits that you set.

function ValidateKey() 
{   
   var key=window.event.keyCode;
   var allowed='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ :;,.?!£$%^&*()_+-*{}@~<>&"\'';

    return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
}

and you can use it as

<input size="30" value="" onkeypress="return ValidateKey();" >​

and on Fiddle: http://jsfiddle/UHGRz/3/

You can applied to all input controls with jQuery. Did not work with copy/paste, there you need the Lenier solution with the Replace.

And a convert to jQuery code

jQuery("input").keypress(function() 
{   
   var key=window.event.keyCode;
   var allowed='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ :;,.?!£$%^&*()_+-*{}@~<>&"\'';

    return allowed.indexOf(String.fromCharCode(key)) !=-1 ;
})​

and on Fiddle : http://jsfiddle/UHGRz/5/

if ch is that char,you can do this

if((ch.charCodeAt(0)>="a".charCodeAt(0) && ch.charCodeAt(0)<="z".charCodeAt(0))||(ch.charCodeAt(0)>="A".charCodeAt(0) && ch.charCodeAt(0)<="Z".charCodeAt(0)))

You can do the same parison with arabic character

which is represented in unicode

[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]

Try something like that. The regex for English characters is /^[A-Za-z0-9]*$/

<input name="yourtextbox" class="englishonly">
<script type="text/javascript">
$('.englishonly').bind('keyup blur',function(){ 
    $(this).val( $(this).val().replace(/^[A-Za-z0-9]*$/g,'') ); }
);
</script>

For email allow only English Characters: (Vuejs)

Input

<input @keypress="onlyEnglish" />
const onlyEnglish = (e) => {
  var regex = new RegExp("^[a-zA-Z0-9@.]+$");
  var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
  if (regex.test(str)) {
      return true;
  }

  e.preventDefault();
  return false;
}

Paste

<input @paste="onlyPasteEnglish" />
const onlyPasteEnglish = (e) => {
  var regex = new RegExp("^[a-zA-Z0-9@.]+$");
  if (regex.test(e.clipboardData.getData('text/plain'))) {
      return true;
  }

  e.preventDefault();
  return false;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论