1

Is there any way to restrict the user from typing the wrong sequence, I mean that user is allowed to type first two character as alphabets only, then the next two as number and the next two as character and then the rest 4 as number. Basically I want to apply restriction on the sequence of the character. The format of the Indian vehicle registration number should not be altered with wrong entries. sample output should be

HR 23 DU 2323

vehicleNumberFormat() {
    const { value } = this.myForm.get('vehicleNumber');
    console.log('value', value);

    const regex = /^(\w{0,2})(\w{0,2})(\w{0,2})(\w{0,4})$/g
    // const onlyNumbers = value.replace(/[^\d]/g, '')
    const typedValue = value.replace(/[^\w]/g, '')

    const formatedVehicleNumber = typedValue.replace(regex, (regex, $1, $2, $3, $4) =>
      [$1, $2, $3, $4].filter(group => !!group).join(' ')
    )

    console.log('formatedVehicleNumber-->', formatedVehicleNumber.toUpperCase())
    this.myForm.controls['vehicleNumber']?.setValue(formatedVehicleNumber.toUpperCase());
    
    return formatedVehicleNumber;
  }

here the codebase stackblitz

Rの卄IT
  • 85
  • 1
  • 3
  • 13

1 Answers1

1

I think following regex should suffice your need

^([a-zA-Z]{2})\s+(\d{2})\s+([a-zA-Z]{2})\s+(\d{4})$
Kousik Mandal
  • 686
  • 1
  • 6
  • 15