0

I am trying to make a jquery validate rule that looks for a specific value pattern. However I am having problems where I get false positives.

here is my rule

Query.validator.addMethod("eidFormat", function (value, element) {
    var a1formatChk = /[^b-z]\d{5}|\d{6}$/.test(value);
    return e1formatChk;
}, "Your oder code needs to be your employee id. Please try again.");

the value I am looking for needs to be a and 5 or 6 digits. Also I am adding the rule on field change. either they need to add their employee id or leave the filed blank and I add a system generated id on the back end.

here is the problem I am having

if I enter b1234 it fails and gives me the message but if I put in garbage like fgkhjkjfhg54645 the test comes back true.

I am thinking this is more a reg ex pattern issue, but am not sure.

any help would be appreciated

1 Answer 1

3

Note that [^b-z] would match any character(including non-word character) but not of b to z. So change your regex to,

^a\d{5,6}$

Explanation:

  • ^ Asserts that we are at the start.
  • a Matches a literal a.
  • \d{5,6} would allow 5 or 6 digits.
  • $ Asserts that we are at the end.
Sign up to request clarification or add additional context in comments.

1 Comment

thank you that worked. I am using w3school for regex pattern writing. is there a good blog or site that goes in to detail?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.