Issue
I am using formik with react And i have a signup page where i need to have the following No special characters Eg Abhi Kumar Abhishek Ray
Etc
So what validation should i place I am using only name field and i want to place it in single text field
So can anyone help
Solution
Here is functional code with test strings:
const names = [
// match:
'Single',
'First Last',
'Frank O',
'Mike McIntosh',
'Eg Abhi Kumar Abhishek Ray',
// no match:
'Anne-Marie Smith',
"O'Brian",
'Müller',
'山本',
' Space At Start',
'Space At End ',
];
const re = /^[A-Z][A-Za-z]*( [A-Z][A-Za-z]*)*$/;
names.forEach(name => {
console.log(name + ' => ' + re.test(name));
});
Output:
Single => true
First Last => true
Frank O => true
Mike McIntosh => true
Eg Abhi Kumar Abhishek Ray => true
Anne-Marie Smith => false
O'Brian => false
Müller => false
山本 => false
Space At Start => false
Space At End => false
Explanation:
^
- anchor at beginning[A-Z]
- expect one uppercase char[A-Za-z]*
- followed by optional upper & lowercase chars(
...)*
- optional pattern (0 to multiple instances) of:[A-Z]
- one uppercase char[A-Za-z]*
- optional upper & lowercase chars
$
- anchor at end- if you want to support
O'Brian
andAnne-Marie
, add'
and-
to the pattern, e.g.[A-Za-z'-]*
Answered By - Peter Thoeny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.