0

In this project I have the following jquery code to validate a form. It works fine, but i'd like it to be more specific by implementing a more thorough validation for UK reg plates. My javascript code is:

function ValidateSspcpForm()
{
  $('#SspcpForm').validate({
    rules: {
      'sspcp[reg]': {
          required: true,
          rangelength: [2, 8],
      },
    messages: {
      'sspcp[reg]': {
        required: "Your car's registration is required in advance, Please input one here.",
        rangelength: "The registration number must be between 2 and 7 characters in length"
      },
    }
  });
}

The method I want to implement is this, it seems to cover everything for UK plates which is perfect for my use case:

(^[A-Z]{2}[0-9]{2}\s?[A-Z]{3}$)|(^[A-Z][0-9]{1,3}[A-Z]{3}$)|(^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(^[0-9]{1,4}[A-Z]{1,2}$)|(^[0-9]{1,3}[A-Z]{1,3}$)|(^[A-Z]{1,2}[0-9]{1,4}$)|(^[A-Z]{1,3}[0-9]{1,3}$)

Any help or ideas would be much appreciated!

  • Welcome to SO! Learning to search is a super power when you're starting out. I searched for "*jquery validation regular expression*" and found many answers, [here's one](https://stackoverflow.com/q/280759/6089612). Some of those answers are for a general regex rule, where you can pass any regex as a param, for any situation; [this answer](https://stackoverflow.com/a/280952/6089612) is less flexible but simpler and matches just one regex. You could name it `ukrego`, swap in your regex, and require it in your rules like `ukrego: true`. – Don't Panic Dec 02 '21 at 21:33
  • Does this answer your question? [jQuery validate: How to add a rule for regular expression validation?](https://stackoverflow.com/questions/280759/jquery-validate-how-to-add-a-rule-for-regular-expression-validation) – Don't Panic Dec 02 '21 at 21:33
  • How will your validation cope with personalised number plates? – Richard Deeming Dec 03 '21 at 12:20
  • 1
    @RichardDeeming As far as i can tell from the site I got the expression from, it appears to include everything, dateless and diplomatic. Tried it with a few, such as the famous "F1", seems to work no problem - [Source Site](https://gist.github.com/danielrbradley/7567269) – Andrew Smart Dec 03 '21 at 13:47

1 Answers1

0

Found it. Definitely appears to be quite simple. I'm new to JavaScript, so wasn't aware it would let you outside of the validate method. For anyone else searching, this is how you would implement UK registration plate validation with jquery.validate:

function ValidateSspcpForm()
{
  $.validator.addMethod('Registration', function(value){
    return /^(^[A-Z]{2}[0-9]{2}\s?[A-Z]{3}$)|(^[A-Z][0-9]{1,3}[A-Z]{3}$)|(^[A-Z]{3}[0-9]{1,3}[A-Z]$)|(^[0-9]{1,4}[A-Z]{1,2}$)|(^[0-9]{1,3}[A-Z]{1,3}$)|(^[A-Z]{1,2}[0-9]{1,4}$)|(^[A-Z]{1,3}[0-9]{1,3}$)|(^[A-Z]{1,3}[0-9]{1,4}$)|(^[0-9]{3}[DX]{1}[0-9]{3}$)/.test(value);
  }, 'reg validation has failed');

  $('#SspcpForm').validate({
    rules: {
      'sspcp[reg]': {
          required: true,
          rangelength: [2, 8],
          Registration: true
      },
    },
    messages: {
      'sspcp[reg]': {
        required: "Your car's registration is required in advance, Please input one here.",
        Registration: "This registration number is not a recognised UK plate"
      },
    }
  });
}
ouflak
  • 2,458
  • 10
  • 44
  • 49