Regular expression for first and last name

后端 未结 24 2377
温柔的废话
温柔的废话 2020-11-22 10:03

For website validation purposes, I need first name and last name validation.

For the first name, it should only contain letters, can be several words with spaces, an

24条回答
  •  星月不相逢
    2020-11-22 10:21

    I'm working on the app that validates International Passports (ICAO). We support only english characters. While most foreign national characters can be represented by a character in the Latin alphabet e.g. è by e, there are several national characters that require an extra letter to represent them such as the German umlaut which requires an ‘e’ to be added to the letter e.g. ä by ae.

    This is the JavaScript Regex for the first and last names we use:

    /^[a-zA-Z '.-]*$/
    

    The max number of characters on the international passport is up to 31. We use maxlength="31" to better word error messages instead of including it in the regex.

    Here is a snippet from our code in AngularJS 1.6 with form and error handling:

    class PassportController {
      constructor() {
        this.details = {};
        // English letters, spaces and the following symbols ' - . are allowed
        // Max length determined by ng-maxlength for better error messaging
        this.nameRegex = /^[a-zA-Z '.-]*$/;
      }
    }
    
    angular.module('akyc', ['ngMessages'])
      .controller('PassportController', PassportController);
     
    .has-error p[ng-message] {
      color: #bc111e;
    }
    
    .tip {
      color: #535f67;
    }
    
    
    
    
    Exactly as it appears on your passport

    Please enter your last name

    This field can be at most 31 characters long

    Only English letters, spaces and the following symbols ' - . are allowed

提交回复
热议问题