1

I am still learning jQuery and I referred to some tutorials online. I got my code from this site and i plugged it into JSFiddle to try but it didn`t work:

http://www.leniel.net/2012/10/javascript-regex-jquery-to-match-only-english-characters-in-input-textbox.html#sthash.XQxbhteA.1FTBwPAJ.dpbs

HTML

<input id="mytextbox" style="width:300px" placeholder="Only English letters are allowed here...">

JS

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

    // Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
    // For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
    var englishAlphabetAndWhiteSpace = /[A-Za-z ]/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
    // englishAlphabetAndWhiteSpace.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 || englishAlphabetAndWhiteSpace.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();
});

The framework I used was No-Library (pure JS) and extension was set to onload.

This script however seems to work on the site I referred to. May I know where I did wrong? Would it be that I did not include some jQuery libraries?

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
aceminer
  • 4,089
  • 9
  • 56
  • 104

3 Answers3

5

As a framework, you need to select a jQuery version (e.g. jQuery 2.1.0) instead of No-Library (pure JS).

If you don't, the jQuery library will not be loaded and hence your jQuery-dependent code will not work.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
2

You must include the jquery files and use it

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

    // Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
    // For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
    var englishAlphabetAndWhiteSpace = /[A-Za-z ]/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
    // englishAlphabetAndWhiteSpace.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 || englishAlphabetAndWhiteSpace.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();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="mytextbox" style="width:300px" placeholder="Only English letters are allowed here...">
Saravanan Rajaraman
  • 1,029
  • 2
  • 12
  • 25
1

Select a jQuery library in the top-left dropdown and it should work ok as per my Fiddle below:

http://jsfiddle.net/Delorian/8b7trd6d/

(copied OP's code)
Delorian
  • 330
  • 1
  • 3
  • 13