Skip to content Skip to sidebar Skip to footer

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) contains only alphabets of lengt

Solution 1:

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

Solution 2:

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.

Solution 3:

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($arras$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";
    elseprint"$str\tNO\n";
}

?>

Output:

RJ-14,NL-1234YESRJ-01,4M-5874YESRJ-07,14M-2345YESRJ07,3M2345   YESRJ-07,3M-8888YESRJ074M2345   YESRJ07,4M2933   YESRJ-073M1234   NORJ-07M3-1234NOrj-07M3-123NO

Post a Comment for "Regular Expression For Indian Vehicle Number In Javascript And Php"