compose a javascript expression that has one uppecase letter and six numbers

120 Views Asked by At

I need to compose a regular JavaScript expression. This expression is based on a serial number The parameters of the expression is this:

One Uppercase Letter at the beginning and the rest of the expression is digits (6)

Example: E234585, C345678, E001234

Thanks for your assistance

3

There are 3 best solutions below

4
Andrew On BEST ANSWER

Try this expression:

/^[A-Z]\d{6}$/

This will match serial numbers in the format you described.

[A-Z] matches the first uppercase letter, then \d{6} matches the following 6 digits. The anchors (^ and $) ensure the matched string contains only the serial number and nothing else.

0
brettkelly On
​var serialre = new RegExp('[A-Z]{1}[0-9]{6}');​​​​​​​​​​​​​​​​​​​​​​​​

if(serialre.test('A123456')){
       document.write('yep');
} else {
    document.write('nope');
}

document.write('<br />');    

if(serialre.test('POOPSTAINS!')){
       document.write('yep');
} else {
    document.write('nope');
}
​

Produces:

yep
nope

Check out the JSFiddle

0
jbabey On

Your question is very unclear, I'm going to take a stab in the dark and assume you meant you wanted to generate those random strings:

var getRandomInt = function (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;  
};

var getRandomLetter = function () {
    return String.fromCharCode(getRandomInt(65, 90));
};

var getRandomDigit = function () {
    return getRandomInt(0, 9);
};

var yourString = getRandomLetter() + getRandomDigit() + ... + getRandomDigit();