French special characters unicode required for first name

61 views Asked by At

In the project that i am working on i want to add enhancement feature where user can add their French name in the first name field.

I want to include all the special characters for French

Right now in the current code, when I am trying to add French characters in the First name, an error is getting thrown.

enter image description here

Please find below JS code validation for First Name. Which exists in the code right now.

var firstNameRegex  = /^[a-zA-Z\u3000\u3400-\u4DBF\u4E00-\u9FFF\-\'\s]{1,45}$/;

I tried for 5 special characters like this

var firstNameRegex  = /^[a-zA-Z\.\u3000\u3400-\u4DBF\u4E00-\u9FFF\u0105\u0107\u0119\u0142\u0144\u00f3\u015b\u017a\u017c\-\'\s]{1,45}$/;

But to include all the characters unicode separately seems bit lengthy Does anyone has any idea how to include all the French Characters with an simpler code.

I tried this combination

        var firstNameRegex  = /^[a-zA-Z\u3000\u3400-\u4DBF\u4E00-\u9FFF\u00C0-\u017F\-\'\s]{1,45}$/;

But this is not working

2

There are 2 answers

6
Fake Rick sanchez On

you can modify regex-

  var firstNameRegex  = /^[a-zA-Z\u00C0-\u017F\.\u3000\u3400-\u4DBF\u4E00-\u9FFF\-\'\s]{1,45}$/;

hope this workd

2
PassThru On

Since you’re a dual-language developer I suggest you get stuck into all JS functions that start with “.locale” or ".toLocal"

Taking advantage of these functions can help you with dates, strings, numbers, currencies, etc, in non-English languages.

Here's an example with strings... (or French names if you prefer)

const a = 'réservé'; // With accents, lowercase

const b = 'RESERVE'; // No accents, uppercase


console.log(a.localeCompare(b));

// Expected output: 1


console.log(a.localeCompare(b,'en', { sensitivity: 'base' }));

// Expected output: 0

This example and further reading on the matter, comes from...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare

Non-English language form validation should embrace such functions instead of regex() and such.