Alphanumeric field validation

  • 14 November 2017
  • 0 replies
  • 4 views

Hi guys,

I must validate an 11-character alphanumeric field as follows:

  • the first character is always an “x”
  • the second character is always a “4”
  • The third and fourth characters are always letters
  • The remaining 7 characters are letters and numbers

Here is an example: X4RT45T5YU3

I’m not a developer, but I have tried to create the following script:

<script>
  lp.jQuery (function ($) {
    var ruleID = 'SN';
    var field = 'serial_number';
    var message = 'Please enter a valid serial number';
    var rules = module.lp.form.data.validationRules [field];
    $ .validator.addMethod (ruleID, function (value, field) {
      var valid = (/[xX]{1}[4]{1}[a-zA-Z]{2}[a-zA-Z0-9]{7}/.test(value));
      return valid || (! rules.required &&! value);
    }, message);
    rules [ruleID] = true;
  });
</ Script>

The script works, but I have the following problem: it can enter more than 11 characters, and I would like the user to get an error message if he inserts more than 11 characters: what am I wrong?

Thank you for your cooperation 🙂


0 replies

Be the first to reply!

Reply