Javascript allow only latin characters

Check if String contains only Latin Letters #

To check if a string contains only latin characters, use the test() method with the following regular expression /^[a-zA-Z]+$/. The test method will return true if the string contains only latin characters andfalse otherwise.

Copied!

function onlyLatinCharacters(str) { return /^[a-zA-Z]+$/.test(str); } console.log(onlyLatinCharacters('apple')); // 👉️ true console.log(onlyLatinCharacters('Buy groceries')); // 👉️ false console.log(onlyLatinCharacters('pizza,cola')); // 👉️ false

If you also need to match spaces, dots, commas, etc, scroll down to code snippet at the bottom of the article.

We used the RegExp.test method to check if a string contains only latin characters.

The test method returns true if the string is matched in the regex and false otherwise.

The forward slashes / / mark the start and end of the regular expression.

The caret ^ matches the beginning of the input and the dollar sign $ - the end of the input.

The part between the square brackets [] is called a character class and matches a range of lowercase a-z and uppercase A-Z letters.

The plus + matches the preceding item (the letter ranges) 1 or more times.

You can also use the i flag to make the regular expression case insensitive. This would allow you to remove the uppercase range A-Z from the square brackets.

Copied!

function onlyLatinCharacters(str) { // 👇️ using `i` flag return /^[a-z]+$/i.test(str); } console.log(onlyLatinCharacters('APPLE')); // 👉️ true

The i flag allows us to do case insensitive search and replaces the uppercase range A-Z.

If you ever need help reading a regular expression, check this regex cheatsheet from MDN out.

If you also need to match dots, commas or spaces, etc, add the character you need to match between the square brackets [].

Copied!

function onlyLatinSpacesDots(str) { return /^[a-zA-Z\s.,]+$/.test(str); } console.log(onlyLatinSpacesDots('apple')); // 👉️ true console.log(onlyLatinSpacesDots('Buy groceries')); // 👉️ true console.log(onlyLatinSpacesDots('pizza.cola')); // 👉️ true

In this example we match latin characters, whitespace characters (\s), dots and commas.

The \s special character matches spaces, tabs and new lines.

You can update this as you need, by adjusting the characters between the square brackets [].

Further Reading #

  • Check if a String is all Uppercase in JavaScript
  • How to count the words in a String in JavaScript

i want has a js code for allow type only latin charter with number and Special Characters(Alt Keyboard Sequences, EX:!@#$%^&*()_|}{~!"?>:'`, ...), i tried like this, but it does not work for latin number and the invisible character Keyboard.what do i do?

DEMO: https://jsfiddle.net/oosjtmgg/

        $(".latin_letters").on("keypress", function(event) {
            var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;
            var key = String.fromCharCode(event.which);
            if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
                return true;
            }
            return false;
        });
        $('.latin_letters').on("paste", function(e) {
            e.preventDefault();
        });

asked Dec 30, 2016 at 6:09

Me hdiMe hdi

1,7345 gold badges23 silver badges34 bronze badges

4

Please try this :

only add:- var englishAlphabetAndWhiteSpace = "/^[-@./#&+\w\s]*$/";

$(".latin_letters").on("keypress", function(event) {
        var englishAlphabetAndWhiteSpace = /^[-@./#&+\w\s]*$/;
            var key = String.fromCharCode(event.which);
            if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
                return true;
            }
            return false;
        });
        $('.latin_letters').on("paste", function(e) {
            e.preventDefault();
        });

answered Dec 30, 2016 at 6:21

Javascript allow only latin characters

Sarika KoliSarika Koli

7636 silver badges11 bronze badges

0

$(".latin_letters").on("keydown", function(event) {
            var allowed = /[A-Za-z0-9!@#\$%\^&\*\(\)_|}{~">:<\?/\]\[\\=\- ]/g;
            var key = String.fromCharCode(event.which);
            if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || allowed.test(key)) {
                return true;
            }
            return false;
        });
$('.latin_letters').on("paste", function(e) {
    e.preventDefault();
});

This will work for you. You had not tested numbers and signals before.

answered Dec 30, 2016 at 6:44

Claim YangClaim Yang

3091 silver badge9 bronze badges

How to check for Latin letters in javascript?

To check if a string contains only latin characters, use the test() method with the following regular expression /^[a-zA-Z]+$/ . The test method will return true if the string contains only latin characters and false otherwise.

What is non Latin characters?

Non Latin Script Languages Of The World.
1) Latin..
2) Greek / Cyrillic..
3) Phoenician..
4) Aramaic / Hebrew / Brahmi / Indic..
5) Arabic..
ARABIC..

How do you check if a string contains letters JS?

To check if a string contains any letters, use the test() method with the following regular expression /[a-zA-Z]/ . The test method will return true if the string contains at least one letter and false otherwise.

What are the letters in the Latin alphabet?

Latin alphabet.