admin管理员组

文章数量:1336397

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?

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?

Share Improve this question asked Nov 13, 2019 at 4:21 DanielDaniel 251 silver badge7 bronze badges 2
  • Are there really only three valid names? Or, are the 3 names you gave just an example of the types of names which are valid? If the latter, then can you also tell us the logic for discerning a correct name from an incorrect one? – Tim Biegeleisen Commented Nov 13, 2019 at 4:25
  • @TimBiegeleisen just examples. The rule consider two or more names, with 3 characters min and 40 maximum. – Daniel Commented Nov 14, 2019 at 2:51
Add a ment  | 

1 Answer 1

Reset to default 7

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'));

本文标签: javascriptHow to do validate full name in JS in React NativeStack Overflow