javascript - Regular expression for unique number.

737

I need a regular expression that validate an unique value. It has a unique 9 digit number, in the format 000000000A (where 0 is a digit and A is a letter). Only allows letter "V" or "X" for last string.

I can do it for digits but not sure how to modify for last string.

^[1-9][0-9]{9}$

Hope somebody may help me out.

164

Answer

Solution:

You can use character class[VX], it'll match a single character from it.

^[1-9][0-9]{8}[VX]$

Or,OR condition as follow

^[1-9][0-9]{8}(V|X)$

Update:

To match the alphabets case-insensitively, usei flag or the lowercase characters can also be added in class

^[1-9][0-9]{8}[VvXx]$
655

Answer

Solution:

You can also try something simpler (unless the leading number cannot be 0)

\d{9}[VX]
59

Answer

Solution:

Why not justd{9} ?/\d{9}(V|X)/ seems easier.

var test = "qwerty000000000Xqwerty";

console.log(test.match(/\d{9}(V|X)/));

http://jsfiddle.net/u5gmfjeL/1/ - matches830363670V as well.

case insensitive add/i -> /\d{9}(V|X)/i -> http://jsfiddle.net/u5gmfjeL/2/

129

Answer

Solution:

You could do:

^[1-9][0-9]{8}[VX]$

Or more generically if the suffix were more than one character you could use groups. As it appears you are using POSIX Extended Regular Expression (ERE) syntax, you can also use parenthesis for groups, like:

^[1-9][0-9]{8}(V|X)$

Link to ERE syntax

Link to JavaScript Regex standard

People are also looking for solutions to the problem: apache - PHP upload_max_filesize not obeyed

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.