-2

I have a jQuery Validate method that check if phone number is input

Now I need to modify it to check if a string contains no phone number

$.validator.addMethod('contains_no_phone_number', function (value, element) {
    return this.optional(element) || /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im.test(value);
}, "Do not include phone number");

I do not want someone to enter string containing phone number in bellow format:

02221234567

0222-1234567

+862221234567

+86-222-123-4567

+86(222)1234567

I want jquery validator method should return

PHONE_NUMBER (False)

PHONE_NUMBER some words (False)

some words PHONE_NUMBER (False)

some words PHONE_NUMBER some words (False)

some words (True)

Phone number lengths are between 7 and above

Note: Feel free to make your own regex if needed.

Sparky
  • 98,165
  • 25
  • 199
  • 285
DMP
  • 523
  • 4
  • 19
  • The pattern that you use `^[0-9\-\(\)\s]+.` can also match 2 spaces, which I think should not be a valid phone number. The question seems a bit confusing to me, as the method name should be `contains_no_phone_number` You might look at [this page](https://stackoverflow.com/questions/4338267/validate-phone-number-with-javascript) for examples how to match a phone number, and then return the opposite of `.test()` when it matches. – The fourth bird May 22 '21 at 09:57
  • @The fourth bird thank you for your time, question is updated, you are right it should not match spaces. – DMP May 22 '21 at 10:16
  • 1
    Given that you've basically asked the same question three times, the only difference being the regex pattern, where is your effort at solving this? In other words, can't you simply combine the logic from the first question with each new regex pattern? Have you tried that? – Sparky May 22 '21 at 21:01
  • @Sparky I am not good in regex, I needed help with regex pattern that is different in each question. – DMP May 24 '21 at 05:35
  • The issue is that no real attempt at a solution was demonstrated. You're simply requesting code based on your criteria. Wiktor even took the time to explain how the regex works in all three solutions so you could learn more about it. – Sparky May 24 '21 at 19:36

1 Answers1

1

Following the logic from our chat, here is the pattern:

/^(?!.*(?:\s|^)(?:\+\d|0)(?:[()-]*\d){6,}(?!\S))/

See the regex demo.

Details:

  • ^ - start of string
  • (?! - start of a negative lookahead:
    • .* - any zero or more chars other than line break chars, as many as possible
    • (?:\s|^) - whitespace or start of string
    • (?:\+\d|0) - + and a digit, or a zero
    • (?:[()-]*\d){6,} - six or more occurrences of zero or more (, ) or - chars and then a digit
    • (?!\S) - no non-whitespace char allowed immediately to the right of the current location
  • ) - end of the lookahead check.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Thank you very much you helped me a lot, I made a mistake in asking the question, please do two modifications – DMP May 24 '21 at 06:05
  • 1) make the regex so that if phone number exists in string it should return false. 2) make it general instead of +86 or 86 any country code could be entered that could be 1,2,3 or 4 digits followed by optional + symbol. – DMP May 24 '21 at 06:07
  • Thank you very much, you are genius, please upvote my questions if possible. You are really really really great. – DMP May 24 '21 at 09:21
  • It returns `false` on `abcd1234567` it should rerun `true` as there is no whitespace in the beginning of number. – DMP May 24 '21 at 09:56
  • 1
    @DMP Ok, so you want to enforce whitespace boundaries, right? Use [this regex](https://regex101.com/r/CglTOX/3) or [this regex](https://regex101.com/r/CglTOX/4) (for any browser) then. – Wiktor Stribiżew May 24 '21 at 10:01
  • What is difference between these two regex, which one you recommend to be used? – DMP May 24 '21 at 18:33
  • 1
    @DMP The second one has no lookbehinds and thus works in Safari and other browsers that do not support lookbehinds yet. – Wiktor Stribiżew May 24 '21 at 19:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232840/discussion-between-dmp-and-wiktor-stribizew). – DMP May 25 '21 at 04:08