Jquery validate: Reuse of a custom number in the USA

I use this:

jQuery.validator.addMethod("regex2", function(value, element, param) {
  return value.match(new RegExp("." + param + "$"));
});

$("#phonenumber").rules("add", { regex2: "[0-9]+"})

to check the phone. The phone number must be at least 7 digits, I received this regular expression from elsewhere and use the parameter minlength=7, but this is clearly unsuitable. How to do it?

I live in Venezuela. Therefore, I think it is useless . How can I change it? In my country, 0416-414 33 44 should be considered as a valid input for a phone number.

+3
source share
4 answers

, , (, , +, -, ., ext) , 7.

$.validator.addMethod('phone', function(value, element) {
    return this.optional(element) || (/^\d{7,}$/).test(value.replace(/[\s()+\-\.]|ext/gi, ''));
});

<input class="phone">.

+2

, :

  • .
  • .
  • .
  • .

"[0-9]+" "[0-9]{7,}\s*$".

, "." "^".

+3

, \s? $ :

  return value.match(new RegExp("." + param + "$"));

? *

+2

. ? , ^, $: , .

, . . :

// Trim value before matching against regex.
return jQuery.trim(value).match(new RegExp("^" + param + "$"));

// Allow spaces at beginning or end with " *" (space + asterisk).
return value.match(new RegExp("^ *" + param + " *$"));

, , , param.

// Add parentheses for robustness.
return jQuery.trim(value).match(new RegExp("^(?:" + param + ")$"));

, , , , this|that. ^(this|that)$, ^this|that$, (^this)|(that$).

+2
source

Source: https://habr.com/ru/post/1764265/


All Articles