-2

I started using jQuery validation on this new project I'm working on, and I've been struggling to find a rule to validate if a text input doesn't start "057" or "089" or "093.

I know there is a notEqual rule that matches the whole input value, is there any way I can achieve the same but only for the first 3 characters?

6
  • 1
    You can use match with jQuery.validator.addMethod() Commented May 7, 2019 at 15:03
  • Thanks @Alex, what's the regex method to match with not starting with "057" or "089" or "093 ?? Commented May 7, 2019 at 15:09
  • 1
    Off the top of my head, something like ^(057|089|093)\d* Not had a chance to test it Commented May 7, 2019 at 15:24
  • Or use the default pattern rule that is part of the additional-methods.js file. Commented May 7, 2019 at 15:44
  • Have you seen RegExr? Commented May 7, 2019 at 15:55

1 Answer 1

0

Try with something like that:

// Custom validation
$.validator.addMethod('customInput', function(value, element) {
    return this.optional(element) || /^(057|089|093)/.test(value);
});
Sign up to request clarification or add additional context in comments.

1 Comment

// Thank you, This worked for me i just had to change the Regex to /^(057|089|093)/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.