function validate(id) {
    var regex = /^[a-zA-Z ]{2,30}$/;
    var ctrl =  document.getElemetnById(id);
    return regex.test(ctrl.value);
}
Answer from Adeel Ahmed on Stack Overflow
๐ŸŒ
CodexWorld
codexworld.com โ€บ home โ€บ how to guides โ€บ how to validate first and last name with regular expression using javascript
How to Validate First and Last Name with Regular Expression using JavaScript - CodexWorld
April 15, 2023 - test() โ€“ This function is used to perform a regular expression match in JavaScript. var regName = /^[a-zA-Z]+ [a-zA-Z]+$/; var name = document.getElementById('nameInput').value; if(!regName.test(name)){ alert('Invalid name given.'); }else{ ...
Discussions

Javascript regex: test people's name - Stack Overflow
Currently it doesn't allow spaces between names at all. I need to be able to match something like John Doe ... Throw any symbols you need in the character class. This is why I said be specific about exactly what you want to validate. This regex will not account for accented characters, if you care about that you'd most likely better go with unicode matching. ... Sign up to request clarification or add additional context in comments. ... and can I do it with javascript... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 25, 2019
regex - JavaScript Regular Expression Validation - Stack Overflow
I'm attempting to validate a field name to match a certain format in JavaScript using Regular Expressions. I need the string inputted to resemble this: word\word\word So anything inputted can't be... More on stackoverflow.com
๐ŸŒ stackoverflow.com
html - How to validate a letter and whitespace only input via JavaScript regular expression - Stack Overflow
I have an input type="text" for names in my HTML code. I need to make sure that it is a string with letters from 'a' to 'z' and 'A' to 'Z' only, along with space(s). ... PS: I'm not really familiar with Regular Expressions, so please do put up an explanation with the code. ... regex Anyway if u are going to store that in a database make sure u do serverside validation as well ... You can use javascript ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript regex (username validation) - Stack Overflow
The real problem here is that a user can simply turn off javascript and submit whatever username they want. please validate this on the backend, too. ... The code you have looks fine, aside from the inconsistent variable reference (see the comment by Josh Purvis). The following regex is fine for your first name ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
PHPpot
phppot.com โ€บ javascript โ€บ validate-name-javascript
How to validate first name and last name in JavaScript? - PHPpot
February 11, 2024 - It creates a Regex pattern that only allows alphabets, spaces, or hyphens for the names. On submitting the form, the first and last names are tested to check if the pattern matches.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ username-validation-in-js-regex
JavaScript - Username Validation using Regex - GeeksforGeeks
January 16, 2025 - function validateUsername(username) ... } // Regex to check valid characters: letters, numbers, dots, underscores const pattern = /^[a-zA-Z0-9._]+$/; if (!pattern.test(username)) { return "Username contains invalid charac...
๐ŸŒ
NYC PHP Developer
andrewwoods.net โ€บ blog โ€บ 2018 โ€บ name-validation-regex
Name Validation Regex for People's Names | NYC PHP Developer | Andrew Woods
September 19, 2018 - This code is in PHP, but it should largely be compatible with JavaScript โ€“ because theyโ€™re both based on Perl โ€“ so you can adapt this to your client side scripts. We need a simple function to do the validation. All we need is a Boolean response. Our initial name validation regex contains something like what most people are currently using.
๐ŸŒ
YouTube
youtube.com โ€บ kv protech
NAME VALIDATION USING REGEX IN JAVASCRIPT - YouTube
EXPLANATION OF NAME VALIDATION USING REGULAR EXPRESSION
Published ย  February 4, 2018
Views ย  1K
๐ŸŒ
Regex Tester
regextester.com โ€บ 97569
Name validation - Regex Tester/Debugger
extended (x) extra (X) single line ... names(J) ... Url checker with or without http:// or https:// Match string not containing string Check if a string only contains numbers Only letters and numbers Match elements of a url date format (yyyy-mm-dd) Url Validation Regex | Regular ...
Find elsewhere
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Regular_expressions
Regular expressions - JavaScript - MDN Web Docs
1 week ago - When you want to know whether a pattern is found in a string, use the test() or search() methods; for more information (but slower execution) use the exec() or match() methods. If you use exec() or match() and if the match succeeds, these methods return an array and update properties of the ...
๐ŸŒ
Medium
emad-uddin.medium.com โ€บ domain-name-validation-using-regex-in-javascript-0a9c2ba342b9
Domain Name Validation Using RegEx in JavaScript | by Emad Uddin | Medium
November 25, 2023 - `domainRegex` can detect the only valid domain in the input text. If you want to check the domain only, you can use this RegEx.
Top answer
1 of 8
21

let result = /^[a-zA-Z ]+$/.test( 'John Doe');
console.log(result);

Throw any symbols you need in the character class. This is why I said be specific about exactly what you want to validate. This regex will not account for accented characters, if you care about that you'd most likely better go with unicode matching.

2 of 8
18

Try this:

/^(([A-Za-z]+[\-\']?)*([A-Za-z]+)?\s)+([A-Za-z]+[\-\']?)*([A-Za-z]+)?$/

It expects optionally [at least 1 alphabetical character followed by a ' or -] an indefinite number of times. There must be at least one alphabetical character before a required space to ensure we are getting at least the first and last name. This entire pattern is grouped to accept indefinite repetition (for people who like to use all their names, such as John Jacob Jingleheimer Schmidt), but must appear at least once, by means of the + sign right in the middle. Finally, the last name is treated the same way as the other names, but no trailing space is allowed. (Unfortunately this means we are violating DRY a little bit.)

Here is the outcome on several possible pieces of input:

"Jon Doe": true
"Jonathan Taylor Thomas": true
"Julia Louis-Dreyfus": true
"Jean-Paul Sartre": true
"Pat O'Brien": true
"รžรณr Eldon": false
"Marcus Wells-O'Shaugnessy": true
"Stephen Wells-O'Shaugnessy Marcus": true
"This-Is-A-Crazy-Name Jones": true
"---- --------": false
"'''' ''''''''": false
"'-'- -'-'-'-'": false
"a-'- b'-'-'-'": false
"'-'c -'-'-'-d": false
"e-'f g'-'-'-h": false
"'ij- -klmnop'": false

Note it still doesn't handle Unicode characters, but it could possibly be expanded to include those if needed.

๐ŸŒ
Javatpoint
javatpoint.com โ€บ regex-for-name-validation-in-javascript
Regex for Name Validation in JavaScript - Javatpoint
Regex for Name Validation in JavaScript - In JavaScript, name validation is a crucial factor of form validation in web development. With the usage of name validation in JavaScript, we make sure that the data entered using the users follows the basic requirements, prevents errors and additionally ...
๐ŸŒ
regex101
regex101.com โ€บ library โ€บ gK4eN5
regex101: Name Validation
RegEx email /^((?!\.)[\w-_.]*)(@\w+)(\.\w+(\.\w+)?)$/gim; Just playing with Reg Ex. This to validate emails in following ways The email couldn't start or finish with a dot The email shouldn't contain spaces into the string The email shouldn't contain special chars ( mailname@domain.com First group takes the first string with the name of email \$1 => (mailname) Second group takes the @ plus the domain: \$2 => (@domain) Third group takes the last part after the domain : \$3 => (.com) Submitted by https://www.linkedin.com/in/peralta-steve-atileon/
๐ŸŒ
YouTube
youtube.com โ€บ watch
Validation In JavaScript | Form Validation In JavaScript Using Regular Expression | SimpliCode - YouTube
๐Ÿ”ฅFull Stack Java Developer Program (Discount Code - YTBE15) - https://www.simplilearn.com/java-full-stack-developer-certification?utm_campaign=QE4EcZ4Dukk&u...
Published ย  September 25, 2021
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ regex-for-name-validation-in-javascript
Regex for Name Validation in JavaScript - Tpoint Tech
March 17, 2025 - What is Name validation in JavaScript? In JavaScript, name validation is a crucial factor of form validation in web development.
Top answer
1 of 6
4

Try /^[a-z]+\\[a-z]+\\[a-z]+$/

function validateResourceName() {
  //get posted resource name value
  var inputString = document.getElementById("resourceName").value;
  //should be in the word\word\word format
  var pattern=/^[a-z]+\\[a-z]+\\[a-z]+$/
  //If the inputString is NOT a match
  if (!pattern.test(inputString)) {
    alert("not a match");
  } else {
    alert("match");
  }
}

If you want to allow the word matching to be case insensitive;

`/^[a-z]+\\[a-z]+\\[a-z]+$/i`

If you want to be a bit more broad with what you define as a 'word', and allow it to consist of alphanumeric characters and underscore;

`/^\w+\\\w+\\\w+$/i`
2 of 6
3

If by word you mean the English letters a-z in upper or lower case, then:

/^(?:[a-z]+\\){2}[a-z]+$/i

That says:

  • ^ Beginning of string
  • (?:...) Non-capturing group
  • [a-z]+ One or more letters a-z (or A-Z because of the i flag at the end). If you also want to allow some other characters, just add them to the [a-z] after the z. If you want to allow hyphens, add \- to it (you need the backslash, depending on where you put the hyphen, so I just always include it). Note that this is very English-centric, and even in English sometimes people write borrowed words with their non-English letters, such as rรฉsumรฉ.
  • \\ Backslash
  • {2} Repeated twice
  • (Then another word)
  • $ End of string

The issues with your expression are:

  • [a-Z] Is invalid because the range is out of order (Z comes before a). If it were valid (or if you wrote [Z-a]), it would matches everything between Z and a, which isn't just a-z and A-Z
  • \\/ Requires a backslash and then a slash
  • | is an alternation (this or that)
  • \s is whitespace
๐ŸŒ
DEV Community
dev.to โ€บ fromwentzitcame โ€บ username-and-password-validation-using-regex-2175
Username and Password Validation Using Regex - DEV Community
June 24, 2023 - In addition to the valid password requirements, a strong password requires at least one uppercase letter, at least one lowercase letter, and at least one special character. We can just build on the same regex statement:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-validate-form-using-regular-expression-in-javascript
JavaScript - How to Validate Form Using Regular Expression? - GeeksforGeeks
To validate a form in JavaScript, you can use Regular Expressions (RegExp) to ensure that user input follows the correct format.
Published ย  December 3, 2024