Regex For A User Name In Javascript A Name Not A Username
Solution 1:
I think this might be the answer:
/** Check if name is valid.
* @param {String} name
* @returns {Boolean}
*/constvalidateName = (name) => {
name = name.trim().replace(/\s+/, ' ')
if (name.length > 30 || name.length < 3) returnfalse// length is too long or too shortif ((name.match(/-/g) || []).length > 2) returnfalse// more than 2 '-' foundif ((name.match(/\s+/g) || []).length > 3) returnfalse// more than 3 'whitespace chains' foundif (name.match(/[~`!@#$%^&*()_=+\d[\]{}\\|:;"'<>,.\/?]/)) returnfalse// not alowed char found
name = name.split(/\s+/g)
for (let part of name)
if (part[0] === '-') returnfalse// part of name starts with -returntrue
}
console.log(`Agnès Jaszenko: ${validateName("Agnès Jaszenko")}`) // validconsole.log(`Ag: ${validateName("Ag")}`) // too shortconsole.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 longconsole.log(` Pan Janusz Kocioł : ${validateName(" Pan Janusz Kocioł ")}`) // validconsole.log(`Maciej Kozieja: ${validateName("Maciej Kozieja")}`) // fancy :3 hah :P - validconsole.log(`-Łarjusz Miętocha: ${validateName("-Łarjusz Miętocha")}`) // starting with -console.log(`Łarjusz@ Miętocha: ${validateName("Łarjusz@ Miętocha")}`) // not alowed charconsole.log(`محمد: ${validateName("محمد")}`) // valid with arabic symbolsconsole.log(`مح1مد: ${validateName("مح1مد")}`) // not valid char found
You can modify this code and return information to user about what is invalid or just use it like this. Hope it works for you :D
How it works?
step 1.trim()
removes all whitespace at begining and end.
step 2.replace(/\s+/, ' ')
replace all whitespace left with exacly one space (doing this allows for 'Name Name'
to be valid and 'reduces length' - actual count of words)
step 3name.length > 30 || name.length < 3
checking the length
step 4name.match(/-/g)
counting '-'
sadly while thers no count matches and simple name.match(/-/g).length would break when no match was found thats why we have to use this 'trick' with 'default value'step 5name.match(/\s+/g)
counting whitespaces
step 6name.match(/[~`!@#$%^&*()_=+\d[\]{}\\|:;"'<>,.\/?]/)
checking if name contains any of not alowed chars
Sadly as you know \d
is not enough to check for all languages but you can reverse this proces and check if name contains any of not allowed chars
step 7 check if part of word begins with -
step 8 if all tests passed your name is valid and true
is returned :)
Live Preview:
/** Check if name is valid.
* @param {String} name
* @returns {Boolean}
*/constvalidateName = (name) => {
name = name.trim().replace(/\s+/g, ' ')
if (name.length > 30 || name.length < 3) returnfalse// length is too long or too shortif ((name.match(/-/g) || []).length > 2) returnfalse// more than 2 '-' foundif ((name.match(/\s+/g) || []).length > 3) returnfalse// more than 3 'whitespace chains' foundif (name.match(/[~`!@#$%^&*()_=+\d[\]{}\\|:;"'<>,.\/?]/)) returnfalse// not alowed char found
name = name.split(/\s+/g)
for (let part of name)
if (part[0] === '-') returnfalse// part of name starts with -returntrue
}
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)
Post a Comment for "Regex For A User Name In Javascript A Name Not A Username"