Regex returns False when validating card

Asked

Viewed 6,001 times

2

I’m trying to validate a car plate in the format : ABC1234 3 letters and 4 numbers.

let regex = new RegExp("/[a-zA-Z]{3}[0-9]{4}/");
let isvalid = regex.test('abc1234');

When I put it on other sites,.

Any idea what it might be? Thank you.

  • 1

    To create regex with the Regexp class you don’t need the bars.

  • Hello, thanks for the help. By taking the bars out, it returns true for any string

  • By adding b,returns false to any entry, I’m testing the code at : http://www.webtoolkitonline.com/javascript-tester.html

  • I used this site , so much so that it was there that I set up the regular expression. But when applying to javascript,.

3 answers

12


If using the class RegExp you do not need to use the bars; these only serve to define a direct regular expression without using the class. If by removing the bars your expression matches other values that should not marry, your expression is wrong. How did you not give details about what the others were strings that the result was true, but should be false, I assumed it would be for values with more than 3 letters followed by more than 4 numbers or variants of it.

let regex = new RegExp("[a-zA-Z]{3}[0-9]{4}");

const tests = [
    'abc1234',
    'ab1234', // falta uma letra
    'abc123', // falta um número
    '1234abc', // começa com número
    'abc12345', // tem um número a mais
    'abcd1234', // tem uma letra a mais
];

for (let test of tests) {
    console.log(test, regex.test(test));
}

If this is, in fact the problem, just fix the expression by adding the characters ^ and $ to define the beginning and end of each value, thus to marry only values that start with 3 letters followed by 4 numbers and nothing more.

let regex = new RegExp("^[a-zA-Z]{3}[0-9]{4}$");

const tests = [
    'abc1234',
    'ab1234', // falta uma letra
    'abc123', // falta um número
    '1234abc', // começa com número
    'abc12345', // tem um número a mais
    'abcd1234', // tem uma letra a mais
];

for (let test of tests) {
    console.log(test, regex.test(test));
}

Using bars instead of class would be:

let regex = /^[a-zA-Z]{3}[0-9]{4}$/;

const tests = [
    'abc1234',
    'ab1234', // falta uma letra
    'abc123', // falta um número
    '1234abc', // começa com número
    'abc12345', // tem um número a mais
    'abcd1234', // tem uma letra a mais
];

for (let test of tests) {
    console.log(test, regex.test(test));
}

It is worth noting that in this case the use of bar notation is preferable because the expression is constant. This makes the code more performatic and more semantic. Prefer to use the class RexExp only when the expression may vary.

  • Thanks,Solved my problem ,I did not know about and $

5

Complementing, you can also delimit using the meta-character \b using literal expression, adding also the ^ that defines the start of the string:

let regex = /^[a-zA-Z]{3}[0-9]{4}\b/;
console.log(regex.test('abc1234')); // correto
console.log(regex.test('abc12345')); // 5 números
console.log(regex.test('abcd2345')); // 4 letras
console.log(regex.test('abc145')); // 3 números
console.log(regex.test('ab2145')); // 2 letras

  • I think the reason it didn’t work when you asked to add b,was that the regular expression /[a-za-Z]{3}[0-9]{4} b/ was in quotes.

  • If it is to use delimiter, I would put before and after. Identify a board in the middle of a string, instead of just at the beginning of the string

2

To validate the current standard and standard (Br) of cards, use this simple script for validation of standard cards and current cards using Javascript (vanilla) Regex:

let plate = "ABC1234";
let plateMerc = "ABC1D23"

const regexPlate = /^[a-zA-Z]{3}[0-9]{4}$/;
const regexPlateMerc = /^[a-zA-Z]{3}[0-9]{1}[a-zA-Z]{1}[0-9]{2}$/;

function validatePlate(plate) {
  if(regexPlate.test(plate)){
    console.warn('Placa válida (padrão atual)');
    return true
  }
  else if(regexPlateMerc.test(plate)){
    console.warn('Placa válida (padrão mercosul)');
    return true
  }
  else {
    console.error('Placa inválida no padrão atual e mercosul');
    return false
  }  
  • Your answer does not include motorcycles that have LLL NN LN standard (where L is letter and N, number).

  • the {1} is unnecessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.