Recently i had to implement a Romanian Phone Number validator… and i have managed to implement it as a method of the jQuery Validation Plugin.

here is the whole method

$.validator.addMethod("phoneRO", function(phone_number, element) {
  phone_number = phone_number.replace(/\(|\)|\s+|-/g, "");
  return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(?:(?:(?:00\s?|\+)40\s?|0)(?:7\d{2}\s?\d{3}\s?\d{3}|(21|31)\d{1}\s?\d{3}\s?\d{3}|((2|3)[3-7]\d{1})\s?\d{3}\s?\d{3}|(8|9)0\d{1}\s?\d{3}\s?\d{3}))$/);
}, "Please specify a valid romanian phone number");

The ReGex of interest is:

/^(?:(?:(?:00\s?|\+)40\s?|0)(?:7\d{2}\s?\d{3}\s?\d{3}|(21|31)\d{1}\s?\d{3}\s?\d{3}|((2|3)[3-7]\d{1})\s?\d{3}\s?\d{3}|(8|9)0\d{1}\s?\d{3}\s?\d{3}))$/

Some of the formats this ReGex is able to recognise are:

00 40 722 000 000
00 40 218 032 329
00 40 243 253 398
00 40 343 254 398
00 40 800 801 227
00 40 318 032 329
0722 000 000
0800 801 227
0800 801227
0318 032 329

Have a try: http://rubular.com/r/2ufyprKWGz

Comments (2)

  1. Vali

    Salut, incerc sa introduc expresia Regex in urmatorul cod, insa nu functioneaza…poti sa imi spui ce ar trebui sa fac pentru a functiona?

    static IDictionary countryRegex = new Dictionary()
    {
    { “USA”, new Regex(“^[2-9]\\d{2}-\\d{3}-\\d{4}$”)},
    { “UK”, new Regex(“(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\\d{4}$)|(^04\\d{2,3}\\d{6}$)”)},
    { “Netherlands”, new Regex(“(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)”)},
    { “Romania”, new Regex(“(/^(?:(?:(?:00\\s?|\\+)40\\s?|0)(?:7\\d{2}\\s?\\d{3}\\s?\\d{3}|(21|31)\\d{1}\\s?\\d{3}\\s?\\d{3}|((2|3)[3-7]\\d{1})\\s?\\d$)”)},
    };

    • alecslupu

      Nu stiu in ce limbaj ai scris snippetul de mai sus. Daca imi dai o idee, as putea sa verific.

Comments are closed.