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?
Asked
Active
Viewed 6,711 times
10

Asef Hossini
- 655
- 8
- 11

Hamid
- 1,948
- 4
- 25
- 38
4 Answers
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
-
why are you using "u0F90-9" ? – Hamid Jan 24 '15 at 08:33
-
2@Hamid Count correctly. He is using `\u06F0-\u06F9` and `0-9`, exactly like you wanted. – Tomalak Jan 24 '15 at 08:43
-
@Hamid consider `\u06F0-\u06F9` and `0-9` as separate parts. Regex engine also parses like this.. – Avinash Raj Jan 24 '15 at 08:44
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
-
better to use`^[\u06F0][\u06F9][\u06F0-\u06F9]{2}[\u06F0-\u06F9]{3}[\u06F0-\u06F9]{4}` in pattern. Cause we have only `۹` in second position. – Asef Hossini Aug 12 '21 at 06:51
-
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