skip to Main Content

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, Rather then adding all the characters unicode I am finding a way where in minimum code I can include all the French characters.

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-Zu3000u3400-u4DBFu4E00-u9FFF-'s]{1,45}$/;

I tried for 5 special characters like this

var firstNameRegex  = /^[a-zA-Z.u3000u3400-u4DBFu4E00-u9FFFu0105u0107u0119u0142u0144u00f3u015bu017au017c-'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.

2

Answers


  1. you can modify regex-

      var firstNameRegex  = /^[a-zA-Zu00C0-u017F.u3000u3400-u4DBFu4E00-u9FFF-'s]{1,45}$/;
    

    hope this workd

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search