-2

So I have this regex to confirm if a name is valid or not, but if I were to add spaces before the name, it will display my error message.

Is there a way to convert my code to validate names like " Bob Joe" if there's extra spaces in the front? But I also wanna make sure nobody can just type "a b joe b bob" and still get a valid name.

Here's my current validation code

 if (!values.name) {
errors.name = 'Name required';
} else if (!/^[A-Za-z]+/.test(values.name)) {
 errors.name = 'Enter a valid name';
}
Brian
  • 488
  • 1
  • 5
  • 20
  • I'd `.trim()` the name first, and then proceed to validate and do other stuff with it – CertainPerformance Aug 30 '20 at 04:53
  • where would I add .trim()? I tried eariler to put it here, but got an error so I'm not sure the correct way to implement it – Brian Aug 30 '20 at 04:54
  • `const name = values.name.trim()` – CertainPerformance Aug 30 '20 at 04:55
  • yeah where would I add that? I tried to add .trim() after values.name yet nothing happened and error still displays when I add spaces – Brian Aug 30 '20 at 04:57
  • You'd add that before you do anything with `values.name`, and proceed to use that variable instead, is what I'd do – CertainPerformance Aug 30 '20 at 04:58
  • but would that prevent someone from randomly typing a name like "bo bb o b b o"? is there a way to prevent that as well? because how would they know it's a first and last name if there's a ton of whitespace – Brian Aug 30 '20 at 04:59
  • How are you defining "a valid name"? Beware of validating too strictly! There's nothing preventing me from changing my name to `a b joe b bob`. https://html.form.guide/best-practices/form-validations-definitive-guide/#:~:text=Validating%20names,have%20more%20than%20three%20parts. https://dev.to/carlymho/whats-in-a-name-validation-4b41 https://softwareengineering.stackexchange.com/questions/330512/name-validation-best-practices Instead of trying to determine first/last name by splitting on a space, etc, just have a field for first name and a field for last name. – WOUNDEDStevenJones Aug 30 '20 at 05:02
  • ah so maybe I should just keep it random then so they can type whatever they want? or is my regex with the letters only good enough? – Brian Aug 30 '20 at 05:04
  • Some names have punctuation, some may even have numbers. If you look at the links I posted, they all generally suggest not trying to validate the contents of a name. https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ is another good list. If anything, just validate if values.name.length > 0 if you want to make sure any name has been entered. But beyond that, you're enforcing your own valid name assumptions that are likely incorrect. https://ux.stackexchange.com/questions/15770/merging-firstname-last-name-into-one-field/15778 is another good reference – WOUNDEDStevenJones Aug 30 '20 at 05:12

1 Answers1

-3

Please try string.trim() function to validate your input.Please click link to better understand https://www.w3schools.com/jsref/jsref_trim_string.asp#:~:text=The%20trim()%20method%20removes,not%20change%20the%20original%20string.