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?

3
  • 3
    1- what have you tried? 2- what are "english" chars? is $ one of them? i know "english" uses it.
    – RASG
    Oct 11, 2012 at 16:43
  • $('input').change(function() { $(this).val('English characters'); }
    – mayhewr
    Oct 11, 2012 at 16:58
  • I have updated the question please review again.............
    – Ram
    Oct 11, 2012 at 17:06

5 Answers 5

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

2
  • 1
    The problem with that is if you try to write it in the middle is move the cursor to the end.
    – Aristos
    Oct 11, 2012 at 17:00
  • See the updated answer with an implementation that solves this annoying problem. Oct 12, 2012 at 16:05
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/

3
  • @Visions I do not have Arabic keyboard to check out, you see if its work.
    – Aristos
    Oct 11, 2012 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, 2012 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, 2012 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]
2
  • Can we also check for Arabic language too
    – Ram
    Oct 11, 2012 at 17:28
  • @Visions test it..i dnt know about arabic..:)
    – Anirudha
    Oct 11, 2012 at 17:48
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>
1
  • 0-9 are arabic numerals. should they be allowed to enter? :)
    – RASG
    Oct 11, 2012 at 16:50
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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.