🌐
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 - <p>Full Name: <input id="name" value=""></p> <input type="button" onclick="validate();" value="Validate Name"> <script> function validate(){ var regName = /^[a-zA-Z]+ [a-zA-Z]+$/; var name = document.getElementById('name').value; if(!regNam...
🌐
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.
Discussions

Regular expression for name field in javascript validation - Stack Overflow
i need a regular expression in javascript validation. Regular expression for name field that will accept alphabets and only space character between words and total characters in the field should be... More on stackoverflow.com
🌐 stackoverflow.com
formvalidation.io - Validations for Password and First name using JavaScript - Stack Overflow
I am doing form validations and the requirement is 1.Email : Email must be a valid Email ID. 2.Password : Must be eight characters long. Only number(0-9) and letters(A-Z,a-z) are a... More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to validate a first name via regex? - Stack Overflow
I'm writing a registration page and I need to ask the user for his first name. I want him to have the option of not capitalizing the first letter but I don't want capital letters inside of his nam... More on stackoverflow.com
🌐 stackoverflow.com
visualforce - How to validate First Name and Last Name in one input textbox using javascript? - Salesforce Stack Exchange
How to validate First Name and Last Name in one input textbox using javascript?i have one inputtext box.when i given first b=name then need to give one space and need to give lastname.when i given ... More on salesforce.stackexchange.com
🌐 salesforce.stackexchange.com
🌐
Medium
a-tokyo.medium.com › first-and-last-name-validation-for-forms-and-databases-d3edf29ad29d
First and Last name validation for forms and databases | by Ahmed Tokyo | Medium
May 5, 2025 - We will start by creating a simple NAME_REGEX regular expression in javascript which allows for "word" characters. We will also create the function isValidName which tests a string against the NAME_REGEX. This function will be run on the first ...
🌐
RegExr
regexr.com › 3f8cm
First/Last Name Validator
Supports JavaScript & PHP/PCRE RegEx. Results update in real-time as you type. Roll over a match or expression for details. Validate patterns with suites of Tests.
🌐
Regex Tester
regextester.com › 93601
First Name - Regex Tester/Debugger
Regular Expression to First name between 1 and 10 characters in length
🌐
TCMHACK
tcmhack.in › blog › how-to-validate-first-and-last-name-with-regular-expression-using-javascript
How to Validate First and Last Name with Regular Expression using JavaScript | TCMHACK
<p>Full Name: <input id="name" value=""></p> <input type="button" onclick="validate();" value="Validate Name"> <script> function validate(){ var regName = /^[a-zA-Z]+ [a-zA-Z]+$/; var name = document.getElementById('name').value; if(!regNam...
🌐
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 - Developers can spend a lot of time ... in JavaScript. Yet, they can fail spectacularly on simpler things like writing decent HTML or allowing people to spell their own names correctly in forms. When your customers cannot correctly write their names in your form, it’s not the customers fault – it’s yours. They know how to spell their names. It’s the name validation regex built into ...
Find elsewhere
🌐
LeadsHook
leadshook.com › home › validating special characters in the first name and last name input fields on a form node using javascript
Validating special characters in the first name and last name input fields on a form node using JavaScript - LeadsHook Knowledge Base
August 8, 2024 - <script> const regexFirstName = /^[a-zA-Z ]*$/; // for first name const regexLastName = /^[a-zA-Z' ]*$/; // for last name, includes single quote let firstName, lastName, submitBtn, buttonTxt, fn, ln; con
Top answer
1 of 3
1

You can use pattern and inbuilt validation of HTML form

  • [^\d]+ - To allow only characters except digits
  • [\dA-Za-z]{8,} - To make sure only digits, and alphabets are allowed and atleast 8 characters

<body>
  <form validate>
    <label for="">First Name</label>
    <input type="text" id="fname" pattern="[^\d]+" required><br>
    <label for="">Last Name</label>
    <input type="text" id="lname"><br>
    <label for="">Email</label>
    <input type="email" id="email" required><br>
    <label for="">Password</label>
    <input type="password" id="password" pattern="[\dA-Za-z]{8,}" required><br>
    <button type="submit">Claim Your Free Trail</button>
    <p>You Are Agreeing to Our <a href="#">Terms & Condition</a></p>
  </form>
</body>

2 of 3
0

Password validation

We use the following regular expression to ensure the password contains only letters and numbers:

/[^A-Za-z0-9]+/g
  • [^] : Matches anything NOT in the set
  • A-Z : Characters in the range "A" to "Z"
  • a-Z: Characters in the range "a" to "z"
  • 0-9: Characters in the range "0" to "9"
  • g: Global flag, allowing inerative search

So if the password contains non-letters and numbers, the regular expression return true.

And the whole password validation function as follow:

function isValidPassword(password) {
    if (
        typeof password !== "string" ||
        password.length !== 8 ||
        /[^A-Za-z0-9]+/g.test(password)
    ) 
        return false;
    return true;
}

Firstname validation

We use the following regular expression to check if the password contains any numbers:

/[0-9]+/g
  • [] : Matches anything IN the set
  • 0-9: Characters in the range "0" to "9"
  • g: Global flag, allowing inerative search

And the whole first name validation function as follow:

function isValidFirstname(firstname) {
    if (
        typeof firstname !== "string" ||
        /[0-9]+/g.test(firstname)
    ) {
        return false; 
    }
    return true;
}
Top answer
1 of 2
3

I have written this function for your purpose.

/**
 * This function checks if first name
 * is valid. Keep in mind this is
 * not the proper solution. It will
 * work only for names written in
 * latin letters.
 * @example
 *   isFirstNameValid('Ivan')
 *   will return true.
 *   isFirstNameValid('IvaN')
 *   will return false.
 * @author Georgi Naumov
 * [email protected] for contacts and
 * suggestions
 **/
const isFirstNameValid = (firstName) => 
  /^[a-zA-z][a-z]+$/.test(firstName)

Edit: I have implemented a solution with unicode support for the people with a similar problem in the future. It supports Latin, Hebrew and Cyrillic. If you want to support another cultures you need to provide regexes for them inside cultures hash.

const isFirstNameValidWithUnicodeSupport = (firstName, culture = 'LATIN') => {
  const cultures = {
    HEBREW: /^[\u0590-\u05FF]{2,}$/,
    CYRILLIC: /^[\u0410-\u042F\u0430-\u044F][\u0430-\u044F]+$/,
    LATIN: /^[A-Za-z][a-z]+$/,
  };
  return  cultures[culture].test(firstName);
}

This returns true because is valid name in latin
alphabet. 
console.log(isFirstNameValidWithUnicodeSupport('Ivan'));
This returns true because is valid name in cyrillic
alphabet. 
console.log(isFirstNameValidWithUnicodeSupport('Иван', 'CYRILLIC'));
This returns false because is valid name in cyrillic
alphabet but there is a space in the end. 
console.log(isFirstNameValidWithUnicodeSupport('Иван ',  'CYRILLIC'));
This returns true because is valid name in hebrew
alphabet. 
console.log(isFirstNameValidWithUnicodeSupport('אגרת',  'HEBREW'));
This returns false because is valid name in hebrew
but is whole name containing spaces. Not only first name.
console.log(isFirstNameValidWithUnicodeSupport('אגרת בת מחלת',  'HEBREW'));

Edit2: Probably better solution is xregexp library if you want to use library for that purpose. https://github.com/slevithan/xregexp

2 of 2
2

You can use regex for matching string characters.

let regex = /^[A-Za-z][a-z]+/g;

let name = "Mark";
let match = regex.exec(name);
if(match && match[0].length === name.length){
  console.log(match[0]); // Mark
}
else{
  console.log("Invalid name");
}

name = "mArk";
match = regex.exec(name);
if(match && match[0].length === name.length){
  console.log(match[0]); 
}
else{
  console.log("Invalid name");
}

To learn more about regex.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › username-validation-in-js-regex
JavaScript - Username Validation using Regex - GeeksforGeeks
January 16, 2025 - {2,15} : The username must be between 3 and 16 characters long (since the first character is already matched, we use {2,15} for the remaining length). ... In some cases, using only regular expressions might not be enough, especially if you have ...
🌐
regex101
regex101.com › library › gK4eN5
regex101: Name Validation
This to validate emails in following ... ( 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › 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   July 23, 2025