Hamid
Hamid

Reputation: 2098

Regex for Persian number

I want to create JavaScript Regex for checking value of textbox in range (U+06F0 to U+06F9) or (0-9) How can I build this?

Upvotes: 14

Views: 8565

Answers (4)

Rasool Aghajani
Rasool Aghajani

Reputation: 441

this pattern is persian Mobile Number for both 09123456789 or ۰۹۱۲۳۴۵۶۷۸۹

 mobileRegExp = /(^09[0-9]{9}$)|(^\u06F0\u06F9[\u06F0-\u06F9]{9})$/

Upvotes: 3

ahmad-mohammadi
ahmad-mohammadi

Reputation: 187

You can test the regex pattern on regexr.com and then use it in your code. I use to match the Persian mobile number with Unicode in blew: <\u06F0 to \u06F9> equal to <۰-۹> that matches Persian number like this: ۰۹۱۹۹۱۹۱۱۲۲.

    $("#registerForm").validate({
        rules:{
            mobile:{
                required:true,
                pattern : ^[\u06F0][\u06F0-\u06F9]{3}[\u06F0-\u06F9]{3}[\u06F0-\u06F9]{4},

            },
        },
        messages:{
            mobile:{
                required:"شماره تلفن همراه خود را وارد کنید",
                number:"فقط عدد وارد کنید",
                pattern:"تلفن همراه را به درستی وارد کنید"
            },
        },
        errorClass: "help-inline",
        errorElement: "span",

    });

Upvotes: 3

Asef Hossini
Asef Hossini

Reputation: 753

I suggest this pattern based on my searches:

pattern = "^([\u06F0]|[0])([\u06F9]|[9])(([\u06F0-\u06F9]|[0-9]){2})(([\u06F0-\u06F9]|[0-9]){3})(([\u06F0-\u06F9]|[0-9]){4})"

It's a little complicated, but works if you want to input both Persian and English numbers in Persian phone number format. I've just used | as or, parentheses for grouping. As @ahmad_mhm mentioned, you can test it on RegExr.

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174756

Put the range inside a character class like below.

^[\u06F0-\u06F90-9]+$

+ repeats the previous token one or more times.

Upvotes: 28

Related Questions