Instead of the below regex:

/^[a-zA-Z]+$/

Use this:

/^[a-zA-Z\s]+$/

This will also take the space.

Answer from Code Lღver on Stack Overflow
🌐
ASPSnippets
aspsnippets.com › questions › 248414 › Validation-to-Allow-only-Alphabets-and-Space-using-Regular-Expression-and-JavaScript
Validation to Allow only Alphabets and Space using Regular Expression and JavaScript
May 2, 2013 - <input type="text" id="txtState" /> <input type="button" id="btnValidate" value="Submit" /> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $("#btnValidate").live("click", function () { var regex = /^[a-zA-Z ]*$/; if (regex.test($("#txtState").val())) { alert("Valid"); } else { alert("Invalid"); } }); </script>
Top answer
1 of 7
89

If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js

You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.

Here's an example:

$("form").validate({
  rules: {
    myField: { lettersonly: true }
  }
});

It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:

jQuery.validator.addMethod("lettersonly", function(value, element) {
  return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please"); 
2 of 7
16

Be careful,

jQuery.validator.addMethod("lettersonly", function(value, element) 
{
return this.optional(element) || /^[a-z," "]+$/i.test(value);
}, "Letters and spaces only please"); 

[a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.

For spaces + text, just do this:

jQuery.validator.addMethod("lettersonly", function(value, element) 
{
return this.optional(element) || /^[a-z ]+$/i.test(value);
}, "Letters and spaces only please");

[a-z ] this allows spaces aswell as text only.

............................................................................

also the message "Letters and spaces only please" is not required, if you already have a message in messages:

messages:{
        firstname:{
        required: "Enter your first name",
        minlength: jQuery.format("Enter at least (2) characters"),
        maxlength:jQuery.format("First name too long more than (80) characters"),
        lettersonly:jQuery.format("letters only mate")
        },

Adam

🌐
Jquery
forum.jquery.com › topic › jquery-validate-allowing-letters-only-in-a-text-input
Jquery
June 26, 2019 - We cannot provide a description for this page right now
🌐
Puneet Sharma
webdevpuneet.com › home › allow alphabets and space only in text field
Allow alphabets and space only in text field - JavaScript
February 14, 2023 - <input type='text' onkeypress="return (event.charCode > 64 && event.charCode < 91) || (event.charCode > 96 && event.charCode < 123) || (event.charCode == 32)" placeholder="Only alphabets and space is allowed"/>
🌐
Monworker
monworker.com › pro-bmx › javascript-validation-to-allow-only-alphabets-and-space.html
Javascript validation to allow only alphabets and space
Some of regular expression given ... With client side validation only if a user disables JavaScript then no nbsp jQuery validation plugin accept only alphabetical characters current file for 1....
Find elsewhere
🌐
JSFiddle
jsfiddle.net › anjankant › fgsfjuhs
Jquery Text:Allow text box only for letters using jQuery? - JSFiddle - Code Playground
Live code validation · Hot reload CSS · Hot reload HTML · Line numbers · Wrap lines · Indent With Spaces · Code Autocomplete · Indent size: 2 spaces · 4 spaces · Font size: Default · Big · Bigger · Jabba · Console in the editor · Clear console on run ·
🌐
Stack Overflow
stackoverflow.com › questions › 24464791 › validate-name-only-with-alphabets-spaces-and-periods-in-jquery-validate-p
regex - Validate name only with 'alphabets', 'spaces' and 'periods' in jQuery Validate plugin - Stack Overflow
$(document).ready(function(){ $("#text").keypress(function (e){ var code =e.keyCode || e.which; if((code<65 || code>90)&&(code<97 || code>122)&&code!=32&&code!=46) { alert("Only alphabates are allowed"); return false; } }); }); ... The OP clearly ...
🌐
.Net Core
technologycrowds.com › 2015 › 11 › how-to-allow-only-alphabets-in-jquery.html
Jquery: How to allow only alphabets in textbox using Jquery - .Net Core | MVC | HTML Agility Pack | SQL | Technology Crowds
how to use JQuery to allow only alphabets in textbox or how to make textbox to allow only alphabets using JQuery, only letters (a-z) using jQuery ... This example will explain how to allow only characters [a/z] in text box using Jquery. It will restrict to write you special characters, functions keys and other keys as well. It will allow backspaces and white spaces while entering the text in input text box.
🌐
YouTube
youtube.com › watch
JQUERY VALIDATION OF WHITE SPACE, ALPHA NUMERIC, LETTER ONLY AND URL - YouTube
White space validation using jqueryAlpha numeric validation using jqueryLetter only validation using jqueryUrl validation using jqueryValidate white space us...
Published   October 10, 2021
🌐
Debugcn
debugcn.com › en › article › 57176872.html
Jquery Validation: allow only alphabets and spaces - DebugCN
February 7, 2021 - My validation accepts only alphabets. I want allow spaces as well. $.validator.addMethod("alpha", function(value, element) { return this.optional(element) || value == value.match(/^[a-zA-Z]+$/); }); ... This will also take the space. ... Please contact [email protected] to delete if infringement.