Home > Backend Development > C++ > How to Create a Regex for Validating Only Persian Characters in Online Forms?

How to Create a Regex for Validating Only Persian Characters in Online Forms?

Linda Hamilton
Release: 2025-01-01 00:39:09
Original
324 people have browsed it

How to Create a Regex for Validating Only Persian Characters in Online Forms?

Regex for Accepting Only Persian Characters

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.

Common Incorrect Approaches

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.

Correct Solution

The correct character set for validating Persian characters includes the following:

  • Letters: [آابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی]
  • Numbers (Persian digits): [۰۱۲۳۴۵۶۷۸۹]
  • Vowels: [ ‬ٌ ‬ًّ ‬َ ‬ِ ‬ُ ‬ْ ‬]

Alternatively, you can use the following codepoint ranges for regular expressions that support codepoint notations:

  • Letters: [u0622u0627u0628u067Eu062A-u062Cu0686u062D-u0632u0698u0633-u063Au0641u0642u06A9u06AFu0644-u0648u06CC]
  • Numbers: [u06F0-u06F9]
  • Vowels: [u202Cu064Bu064Cu064E-u0652]

Why Avoid u0600-u06FF and [آ-ی]?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template