Наша задача написать функцию которая будет возвращать ошибку в случаи если в поле ИНН клиента содержится любой символ кроме числа.
Задача на первый взгляд достаточно простая. Делаем тип поля число и вопрос решен, но не все так просто.
ИНН клиента юр. лица может начинаться с числа ’0′ и как мы помним из уроков математики ’0′ в начале числа убирается. :( Учитывая это нам необходимо делать тип поля текст что в свою очередь разрешает запись любого символа.
Пример ниже проверяет каждый символ входящей переменной используя регулярные выражения. В случаи если символ не число формирует ошибку и выводит её в дальнейшем.
function CheckINN(&text) { var rExp=""; var v_i1 = 0; var msgText = ""; //Объявляем переменные которые можно использовать в строке rExp = new RegExp(/[0-9]/); //Проверяем необходимое количество символов в строке. msgText += CheckLenght(text,10,"Y"); //Цикл по буквам в строке for (var i =0; i < text.length; i++) { //Проверяем символ if ((!rExp.test(text.charAt(i)))) { v_i1++; msgText += " Разрешено использовать только 'цифры'."; break; } } return msgText; }
Как вариант можно использовать следующее:
if (text.match(/^[\d]{10}$/) == null) msgText += «ИНН должен содержать только 10 цифр»;
Element Matches this…
[xyz] Any one character x or y or z, as listed in square brackets
[x-z] Any one character within the range specified in brackets
[^xyz] Any one character except x, y, z
[^x-z] Any one character except the range in brackets
. Any character except newline
\d \D Any digit (\d), any non-digit (\D)
\s \S Any whitespace (\s), any non-whitespace (\S)
\w \W Any word character (\w), any non-word character (\W)
\b \B Word boundary (\b), non-boundary (\B)
[\b] Backspace (the square brackets help distinguish backspace from word boundary)
^ Beginning of string; beginning of line if the m flag is set
$ End of string; end of line if the m flag is set
\n \r \f \t \v Newline, carriage return, form feed, tab, vertical tab
\+ \- \. \* \? \| Match + - . * ? | etc. verbatim (not as a special character)
\/
\^ \$ Match / \ ^ $ verbatim (not as a special character)
\[ \] \( \) \{ \} Match brackets/parentheses/braces verbatim
\xAB Match the character with hexadecimal code AB
\uABCD Match the Unicode character with hexadecimal code ABCD
x|y Match x or y
+ Match the preceding element one or more times
* Match the preceding element zero or more times
? Match the preceding element zero or one time
{n} Match the preceding element exactly n times
{n,} Match the preceding element n or more times
{m,n} Match the preceding element at least m, at most n times
(…) Match … as a capturing group: store the matching substring
(used with string.match(re) to return an array of matching strings)\\
\1 \2 \3 etc. Match the same substring as in capturing group number 1, 2, 3 etc.
$1 $2 $3 etc. Replace with the characters that matched group number 1, 2, 3 etc.
(used in the second argument of replace() method)\\
(?:…) Match … as a non-capturing group: grouping only, no storing
Any character Match the character verbatim
(except the above)
RegExp flags can be any combination of the following:
i Ignore case (both lowercase and uppercase letters will match)\\ g Global (allow multiple matches)\\ m Multiline (^ and $ will match the beginning/end of lines)\\
The effect of not setting these flags is as follows:
i not set The regular expression is case-sensitive
g not set Use only the first match (no global search)
m not set ^ and $ match the beginning/end of the entire string
Notes
(1) A whitespace (\s) in a regular expression matches any one of the characters: space, formfeed (\f), newline (\n), return (\r), tab (\t), vertical tab (\v), non-breaking space (\xA0), as well as the Unicode characters \u00A0 \u2028 \u2029.
(2) When using the RegExp constructor: for each backslash in your regular expression, you have to type
in the RegExp constructor. (In JavaScript strings,
represents a single backslash!) For example, the following regular expressions match all leading and trailing whitespaces (\s); note that \\s is passed as part of the first argument of the RegExp constructor:
re = /^\s+|\s+$/g re = new RegExp('^\\s+|\\s+$','g') Copyright © 1999-2011, JavaScripter.net.