-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkFixedTelephoneNumber.ts
57 lines (53 loc) · 1.7 KB
/
checkFixedTelephoneNumber.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { CheckResult } from "../checkResult";
import { CheckParamsBase } from "../checkParamsBase";
import { isNull } from "../util";
import { checkCanNullOrEmpty, checkRegs } from "./checkCommon";
//正则
let regList = [
// /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/ // 固定电话号码
/^(0[0-9]{2,3}\-{0,1})?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/ // 固定电话号码
];
/**
* 固定电话检查参数
*
* @export
* @class CheckFixedTelephoneNumberParams
* @extends {CheckParamsBase}
*/
export class CheckFixedTelephoneNumberParams extends CheckParamsBase {}
/**
* 检查一个字符串是否符合固定电话格式
*
* @export
* @param {string} value - 被检查的字符串
* @param {CheckFixedTelephoneNumberParams} [params] - 参数
* @returns {CheckResult}
* @example
* <br/><br/>
* <pre>
* import { isFixedTelephoneNumber } from "check-string";
* ...
* let str = "010-22345678"
* let result = isFixedTelephoneNumber(str, { canNull: true }); //str可以为null
* if(!result.success) {
* console.log(result.error.code); // 输出错误编号
* console.log(result.error.cn); // 输出错中文错误
* console.log(result.error.en); // 输出错英文错误
* }
* </pre>
*/
export function isFixedTelephoneNumber(value: string, params?: CheckFixedTelephoneNumberParams): CheckResult {
let resultCanNullOrEmpty = checkCanNullOrEmpty(value, params);
if (resultCanNullOrEmpty) {
return resultCanNullOrEmpty;
}
if (checkRegs(regList, value)) {
return new CheckResult(true);
} else {
return new CheckResult(false, {
code: `ERROR_NOT_TEL`,
en: `please enter a valid fixed phone number`,
cn: `请输入一个有效的固定电话号码`
});
}
}