开发者

regular expression for indian vehicle number in javascript and php

I need a regular expression in Javascript, as well as in PHP, for Indian vehicle NUMBER.

Here is the conditions list:

(x)(y)(z)(m)(a)(b)(c)

1. (x) co开发者_开发技巧ntains only alphabets of length 2.

2. (y) may be - or single space ' '.

3. (z) contains only numbers of length 2.

4. (m) may be or , or single space ' '.

5. (a) must be one or two alphanumeric followed by an alpha.

6. (b) must be identical to (y).

7. (c) contains only numbers of length 4.

Here are some examples of valid vehicle numbers:

  • RJ-14,NL-1234
  • RJ-01,4M-5874
  • RJ-07,14M-2345
  • RJ 07,3M 2345
  • RJ-07,3M-8888
  • RJ 07 4M 2345
  • RJ 07,4M 2933

and some invalid ones (with reason):

  • RJ-07 3M 1234 (both (y) and (b) should be same).
  • RJ-07 M3-1234 ((a) must ends with alphabet).
  • rj-07 3M-123 (length of (c) must be 4).


Here's the regex... this should be safe in most/all langauges:

([a-z]{2}-\d{2}[ ,][a-z0-9]{1,2}[a-z]-\d{4})|([a-z]{2} \d{2}[ ,][a-z0-9]{1,2}[a-z] \d{4})

The reason why I have the regex repeated twice with an "or" in the middle is to meet your criteria that "both (y) and (b) should be same."


You don't need to solve every problem with a regular expression, you can quite easily check it with code. All but number 5 are easy, so you could use:

^[A-Z]{2}[ \-][0-9]{2}[ ,][A-Z0-9]{2,3}[ \-][0-9]{4}$

then check characters 7 and 8 (and 9 if the total length is 14 rather than 13) for condition number 5. And also check the positions 3 and 7 are identical.

The code needed to check this old style is likely to be much more readable (and maintainable) than a regular expression to do the same thing.


On re-reading the question, there appears to be confusion in conditions 5 and 6. Condition 5 makes it sound like any of the two or three characters can be alpha whereas your second example indicates the last must be alpha.

Condition 6 use of the word similar indicates the condition is similar whereas your first example indicates the characters must be identical.

If the examples are correct, you can use:

^[A-Z]{2}([ \-])[0-9]{2}[ ,][A-Z0-9]{1,2}[A-Z]\1[0-9]{4}$

(adjusting if you need lower case as well) but I still maintain that well laid out non-regex code is more maintainable.


Try:

<?php
$arr = array("RJ-14,NL-1234", "RJ-01,4M-5874", "RJ-07,14M-2345", "RJ 07,3M 2345", "RJ-07,3M-8888", "RJ 07 4M 2345", "RJ 07,4M 2933","RJ-07 3M 1234","RJ-07 M3-1234","rj-07 M3-123");

foreach($arr as $str) {
    if(preg_match('/[a-z]{2}( |-)\d{2}(?: |,)(?:[a-z\d]{1,2}[a-z])\1\d{4}/i',$str))
        print "$str\tYES\n";
    else
        print "$str\tNO\n";
}

?>

Output:

RJ-14,NL-1234   YES
RJ-01,4M-5874   YES
RJ-07,14M-2345  YES
RJ 07,3M 2345   YES
RJ-07,3M-8888   YES
RJ 07 4M 2345   YES
RJ 07,4M 2933   YES
RJ-07 3M 1234   NO
RJ-07 M3-1234   NO
rj-07 M3-123    NO
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜