Regex for username in javascript name not username

I tried with this for a while and could not, not so well with regex, I need the following:

Username (person name is not username)

it may include - because some names have a hyphen in them (maximum 2 is allowed)

it may include a space (up to 3)

it cannot include numbers and must begin with a non-hyphen letter

must be at least 3 characters and 30 max

it must match people with non-English letters (e.g. Agnès)

I tried this, but it failed, it allowed me to start with a number and not accept non-English letters like è or ï

^[a-zA-Z\-]{3,30}$

Any help would be greatly appreciated, and I think this will help many programmers looking for SO

0
1

, :

/** Check if name is valid.
 * @param {String} name
 * @returns {Boolean}
 */
const validateName = (name) => {
    name = name.trim().replace(/\s+/, ' ')
    if (name.length > 30 || name.length < 3) return false // length is too long or too short
    if ((name.match(/-/g) || []).length > 2) return false // more than 2 '-' found
    if ((name.match(/\s+/g) || []).length > 3) return false // more than 3 'whitespace chains' found
    if (name.match(/[~`!@#$%^&*()_=+\d[\]{}\\|:;"'<>,.\/?]/)) return false // not alowed char found
    name = name.split(/\s+/g)
    for (let part of name)
        if (part[0] === '-') return false // part of name starts with -

    return true
}

console.log(`Agnès Jaszenko: ${validateName("Agnès Jaszenko")}`) // valid
console.log(`Ag: ${validateName("Ag")}`) // too short
console.log(`Aga-ta roko-la-foo: ${validateName("Aga-ta roko-la-foo")}`) // too many -
console.log(`Agata Jolanta Krzyżtofska-Brzęczyszczewiczykówna: ${validateName("Agata Jolanta Krzyżtofska-Brzęczyszczewiczykówna")}`) // to long
console.log(`   Pan     Janusz   Kocioł  : ${validateName("   Pan     Janusz   Kocioł  ")}`) // valid
console.log(`Maciej Kozieja: ${validateName("Maciej Kozieja")}`) // fancy :3 hah :P - valid
console.log(`-Łarjusz Miętocha: ${validateName("-Łarjusz Miętocha")}`) // starting with -
console.log(`Łarjusz@ Miętocha: ${validateName("Łarjusz@ Miętocha")}`) // not alowed char
console.log(`محمد: ${validateName("محمد")}`) // valid with arabic symbols
console.log(`مح1مد: ${validateName("مح1مد")}`) // not valid char found

, , . , : D

?

1 .trim() .
2 .replace(/\s+/, ' ') , ( 'Name Name' " " - )
3 name.length > 30 || name.length < 3
4 name.match(/-/g) '-'
, thers .match(/-/g).length , , "" " "
5 name.match(/\s+/g)
6 name.match(/[~`!@#$%^&*()_=+\d[\]{}\\|:;"'<>,.\/?]/) , - , , \d , , - 7 , -
8, , true:)

:

/** Check if name is valid.
 * @param {String} name
 * @returns {Boolean}
 */
const validateName = (name) => {
    name = name.trim().replace(/\s+/g, ' ')
    if (name.length > 30 || name.length < 3) return false // length is too long or too short
    if ((name.match(/-/g) || []).length > 2) return false // more than 2 '-' found
    if ((name.match(/\s+/g) || []).length > 3) return false // more than 3 'whitespace chains' found
    if (name.match(/[~`!@#$%^&*()_=+\d[\]{}\\|:;"'<>,.\/?]/)) return false // not alowed char found
    name = name.split(/\s+/g)
    for (let part of name)
        if (part[0] === '-') return false // part of name starts with -

    return true
}

const input = document.createElement('input')
const valid = document.createElement('p')
input.addEventListener('keyup', () =>{
  valid.innerHTML = validateName(input.value)
})
document.body.append(input)
document.body.append(valid)
0

Source: https://habr.com/ru/post/1669449/


All Articles