1

I need to add custom validation for a Name field. The name however needs to be camelCase. I need to add a check to see if its camelCase else throw an appropriate error message. I am not sure how to check for camelCase. Any help will be much appreciated. Really stuck with this one.

Sidd
  • 84
  • 1
  • 2
  • 7
  • do you have some examples and the wanted result? – Nina Scholz Sep 03 '19 at 17:55
  • seems like a job for regex https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions – Marc Sloth Eastman Sep 03 '19 at 18:00
  • who would you know it is not camel case? – epascarello Sep 03 '19 at 18:00
  • You need the user to enter something in camelcase? That's a bit unusual. –  Sep 03 '19 at 18:04
  • Does this help? [Camel Case on RegExr](https://regexr.com/2rilj) – admcfajn Sep 03 '19 at 18:05
  • 1
    This doesn't sound possible. How would you even be able to tell when the next word starts? You can probably just check for no space/special characters and check if the first letter is lowercase. Although I do agree with @Amy, that is pretty unusual and not very user friendly to force the user to enter the name in camelcase. What exactly is the use case here? – nash11 Sep 03 '19 at 18:08

1 Answers1

2

Simply check for no whitespace, underscore, or other undesired symbols.

// returns false if name includes whitespace, _, or -.
let validate = name => !name.match(/[\s_-]/g);

console.log(validate('yoBro')); // true
console.log(validate('yo bro')); // false
console.log(validate('yo_bro')); // false
console.log(validate('yo-bro')); // false
junvar
  • 11,151
  • 2
  • 30
  • 46