Home > front end >  yup form validation with formik using regex not working as expected with bangla font input field
yup form validation with formik using regex not working as expected with bangla font input field

Time:05-29

I write a regex code in registration model but when i input in formik form field with bangla font, regex express can't validate bangla unicode value.

import * as Yup from "yup";
const banglaLang = /^([A-Za-z]|\p{InBengali})$/i;
class RegisterModel {
  /**
   * Model properties
   */
  constructor() {
    this.employeeNameBn = "";
  }

  /**
   * Validator schema
   */
  validator() {
    return Yup.object().shape({
      employeeNameBn: Yup.string()
        .required("user full name (in Bangla) is a required field")
        .matches(banglaLang, "Please type in bangla"),
    });
  }
}

export const Register = new RegisterModel();

CodePudding user response:

You can use

/^[\p{sc=Bengali}\s] $/u;

Details:

  • ^ - start of string
  • [\p{sc=Bengali}\s] - one or more occurrences of
    • \p{sc=Bengali} - a Bengali char
    • \s - a whitespace char
  • $ - end of string
  • u - a flag that allows the use of Unicode property classes like \p{...}.

See the regex demo.

  • Related