Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 321
Hamid Avatar asked Jan 24 '15 08:01

Hamid


3 Answers

Put the range inside a character class like below.

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

+ repeats the previous token one or more times.

like image 155
Avinash Raj Avatar answered Oct 12 '22 22:10

Avinash Raj


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",

    });
like image 31
ahmad_mhm Avatar answered Oct 13 '22 00:10

ahmad_mhm


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.

like image 21
Asef Hossini Avatar answered Oct 13 '22 00:10

Asef Hossini