3

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?

Ram
  • 1,131
  • 10
  • 28
  • 52

5 Answers5

4

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.net/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.org/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.com/q/3050984/114029
    var key = String.fromCharCode(event.which);

    //alert(event.keyCode);

    // For the keyCodes, look here: http://stackoverflow.com/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

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
2

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.net/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.net/UHGRz/5/

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • @Visions I do not have Arabic keyboard to check out, you see if its work. – Aristos Oct 11 '12 at 17:24
  • try any keyword on google translator and paste that in textbox... your code is not working if i enters Arabic chars – Ram Oct 11 '12 at 17:30
  • @Visions I have write, is not work for copy paste. this is for entering data with the keyboard. You ask "typed in my web page" – Aristos Oct 11 '12 at 17:31
1

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 comparison with arabic character

which is represented in unicode

[\u0600-\u06ff]|[\u0750-\u077f]|[\ufb50-\ufc3f]|[\ufe70-\ufefc]
Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

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>
Max
  • 2,529
  • 1
  • 18
  • 29
0

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;
}