In online form validation, it is often necessary to restrict user input to specific character sets. When dealing with Persian text, a common challenge is ensuring that only Persian characters are accepted, excluding all others.
Some attempts to validate Persian characters using regular expressions rely on the codepoint range u0600-u06FF. However, this range encompasses Arabic characters as well, leading to false positives. Similarly, relying solely on the character range [آ-ی] also fails to account for all Persian characters.
The correct character set for validating Persian characters includes the following:
Alternatively, you can use the following codepoint ranges for regular expressions that support codepoint notations:
Despite including the required Persian characters, both u0600-u06FF and [آ-ی] are inaccurate and inefficient. u0600-u06FF includes numerous irrelevant characters, such as Arabic extended letters, diacritics, and punctuation. [آ-ی] also contains characters beyond the necessary Persian alphabet.
By using the correct character set, you can ensure that only authorized Persian characters are permitted in your form field, enhancing data accuracy and consistency.
The above is the detailed content of How to Create a Regex for Validating Only Persian Characters in Online Forms?. For more information, please follow other related articles on the PHP Chinese website!