Daniel
Daniel

Reputation: 25

How to do validate full name in JS in React Native

I have a simple form for signup to my app in React Native. In one step, im validate the name of user is full. Initially, i writing a regex for validate characters and size min and max, but i need validate of a structure of name, with the before rules.

Examples
Valid names: Luke Skywalker, Ben Skywalker, Lu Skywalker
Invalid names: L Skywalker, Luke

My regex start here:

const rule = /^[a-zA-Z ]{2,40}$/;

How i would should write this regex? Grouping these rules?

Upvotes: 1

Views: 7505

Answers (1)

Samuel Goldenbaum
Samuel Goldenbaum

Reputation: 18909

You can try the following as a start: ^[a-zA-Z]{2,40} [a-zA-Z]{2,40}$

const pattern = /^[a-zA-Z]{2,40}( [a-zA-Z]{2,40})+$/;

console.info(pattern.test('Luke Skywalker'));
console.info(pattern.test('Ben Skywalker'));
console.info(pattern.test('Lu Skywalker'));

console.info(pattern.test('Lu Saber Skywalker'));
console.info(pattern.test('Ben The Ghost Skywalker'));

console.info(pattern.test('L Skywalker'));
console.info(pattern.test('Luke'));

Upvotes: 7

Related Questions