JS function to allow enter only letters and white spaces

javascript check if string contains only letters
javascript alphabet
allow only alphabets and space in textbox using jquery validation
javascript regex only letters, numbers and spaces
javascript check if string contains uppercase and lowercase
regex only letters and spaces
allow only alphabets and space in textbox using angular
javascript validation for alphabets only

I need a jquery or js function to only allow enter letters and white spaces. Thanks in advance.

page:

<p:inputText onkeypress="onlyLetter(this)">

function:

function onlyLetter(input){
    $(input).keypress(function(ev) {
   var keyCode = window.event ? ev.keyCode : ev.which;
  //  code

    });
}
Note: KeyboardEvent.which is deprecated as of Jan. 1, 2020

Just use ascii codes (decimal values) of keys/digits that you want to disable or prevent from being work. ASCII Table .

HTML :

<input id="inputTextBox" type="text" />

jQuery :

$(document).ready(function(){
    $("#inputTextBox").keydown(function(event){
        var inputValue = event.which;
        // allow letters and whitespaces only.
        if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) { 
            event.preventDefault(); 
        }
    });
});

jsFiddle Demo

Regex for names validation allow only letters and spaces, jQuery.validator.addMethod("letterswithspace", function(value, element) { return this.optional(element) || /^[a-z][a-z\s]*$/i.test(value); }, "letters� I need a jquery or js function to only allow enter letters and white spaces. Thanks in advance. page: &lt;p:inputText onkeypress="onlyLetter(this)"&gt; function: function onlyLetter(input){ $(

The following code allows only a-z, A-Z, and white space.

HTML

<input id="inputTextBox" type="text" />

jQuery

$(document).on('keypress', '#inputTextBox', function (event) {
    var regex = new RegExp("^[a-zA-Z ]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
        event.preventDefault();
        return false;
    }
});

javascript onkeypress allow only alphabets and white space, I want to make a function to allow user to enter only alphabets and single spaces between them. I've written a function which allows only� JavaScript function to allow only AlphaNumeric (Alphabets and Numbers) characters and space in TextBox. Initially an Array is created which stores the ASCII Key Codes of all the allowed Special Characters. Note: For more details on various Keyboard key ASCII code please visit Keyboard Keys and Key Code Values. Inside the IsAlphaNumeric JavaScript function, first the KeyCode of the pressed Keyboard key is determined and then it is matched with the ASCII Key Codes values of.

First off, I have little experience in jQuery and will provide a vanilla javascript example. Here it is:

document.getElementById('inputid').onkeypress=function(e){
    if(!(/[a-z ]/i.test(String.fromCharCode(e.keyCode))) {
        e.preventDefault();
        return false;
    }
}

JavaScript : Checking for all letters, Javascript function to check if a field in a html form contains all letters or not. we use a regular expression (/^[A-Za-z]+$/) which allows only letters. Next the <div class="mail"> <h2>Enter your Name and Submit</h2> <form� Only numeric input should be allowed for some fields. So, user can’t enter non-numeric characters in those particular fields. One step further, user can’t even copy-paste non-numeric input. Only Character Limited to alphabetic character input only with an exception i.e. space character that may require even if we are entering only alphabets.

Tweaking Ashad Shanto answer a bit. Notice you cant type in y and z if you use the script. You have to change the inputValue from 120 to 123. Here is the ASCII table reference: http://ee.hawaii.edu/~tep/EE160/Book/chap4/subsection2.1.1.1.html Use the script below to type in all the letters, space and backspace.

<script>
    $(document).ready(function(){
        $("#inputTextBox").keypress(function(event){
            var inputValue = event.which;
            // allow letters and whitespaces only.
            if(!(inputValue >= 65 && inputValue <= 123) && (inputValue != 32 && inputValue != 0)) { 
                event.preventDefault(); 
            }
            console.log(inputValue);
        });
    });

</script>

Allow only alphabets in a textbox using javascript, <html> <head> <title>allwon only alphabets in textbox using JavaScript</title> < script language="Javascript" type="text/javascript"> function onlyAlphabets(e, t) { Javascript function to check if a field input contains letters and numbers only. To get a string contains only letters and numbers (i.e. a-z, A-Z or 0-9) we use a regular expression /^ [0-9a-zA-Z]+$/ which allows only letters and numbers.

you could use this simple method, that I took from this post

<input type="text" name="fullName" onkeypress="return (event.charCode > 64 && 
event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)" 
placeholder="Full Name">

Regular Expression (Regex) to accept only Alphabets and Space in , On the OnClientClick event of the Button, a JavaScript function is executed which validates the TextBox text against the Regular Expression (Regex) and if it� Sorry for this late reply. Try this: add ending semi-colons to lines 42, 49, 56, 67, 79, 91, 106, and 113. then ending bracket on line 40 does not match any beginning bracket so just comment out

[Solved] Jquery Validation: allow only alphabets and spaces , Try this: Hide Copy Code. ^[a-zA-Z\s]+$ \s for one space. Read more: The 30 Minute Regex Tutorial[^]. To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[A-Za-z]+$/) which allows only letters. Next the match() method of string object is used to match the said regular expression against the input value.

Jquery Text:Allow text box only for letters using jQuery?, Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor. Whitespace is preserved by the browser. Text will only wrap on line breaks. Acts like the <pre> tag in HTML: Play it » pre-line: Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks: Play it » pre-wrap: Whitespace is preserved by the browser. Text will wrap when necessary, and on

JS with no special characters and blank spaces - JavaScript, Just if used, no special characters and no blank spaces. (Hint: It's easier to specify what IS allowed, rather than what ISNT) The Javascript function String. match is used to check whether a string matches the regex supplied� Splitting Strings. JavaScript has a very useful method for splitting a string by a character and creating a new array out of the sections. We will use the split () method to separate the array by a whitespace character, represented by " ".

Comments
  • Uh. Don't bind a keypress handler in a keypress handler. Use one or the other (preferably the non-inline form setup in $(readyFn)).
  • See stackoverflow.com/a/16833636/2864740 for ideas of what the "code" might look like. Adjust as needed.
  • Letters, not digits :)
  • In this code, you can input characters like !,@,#,$,%,^,&,* etc.
  • Here's my updated code above to prevent catching sysmbols(!,@,#, etc). Also backspace and enter key enabled. if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0)) { event.preventDefault(); } jsfiddle.net/lucerosama/5x7td5bc
  • This is old but I came across it today. You're cutting off lowercase y and z (121 and 122).
  • Worked like a charm. Thank you.
  • Won't take care of uppercase letters.
  • @Libin what about nooowww
  • A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.
  • Okay Thanks for the suggestion
  • Please format the code properly and explain how this is different from all the other answers posted 5 years ago.
  • if we are calling the function in key press event it will only allow the alpha character with ' code it will allow.