10

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?

Asef Hossini
  • 655
  • 8
  • 11
Hamid
  • 1,948
  • 4
  • 25
  • 38

4 Answers4

25

Put the range inside a character class like below.

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

+ repeats the previous token one or more times.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
3

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

    });
ahmad-mohammadi
  • 157
  • 2
  • 5
3

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.

Asef Hossini
  • 655
  • 8
  • 11
2

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

 mobileRegExp = /(^09[0-9]{9}$)|(^\u06F0\u06F9[\u06F0-\u06F9]{9})$/
Rasool Aghajani
  • 340
  • 2
  • 10