我需要验证 phone 数字中的前四位数字,这在 Node Js 中应该是“5678”。我应该怎么做?我应该使用哪个验证器库

I need to validate first four digits in a phone number which should be "5678" in Node Js. How should i do it? Which Validator library should i use

我需要验证 phone 数字中的前四位数字,在 nodeJs 中应该是“5678”我应该怎么做。我应该使用哪个验证器库

我认为最好只在需要时才依赖外部库。

对于您的情况,您可以使用非常简单的正则表达式检查您的 phone 号码是否以“5678”开头:

const validate_phone = /^5678/

var phone_number = '5678122535'
console.log(validate_phone.test(phone_number))  // true

var phone_number = '1567812253'
console.log(validate_phone.test(phone_number))  // false

^代表matches beginning of input

test代表executes a search for a match between a regular expression and a specified string

您可以使用子字符串函数,它在复杂性方面比正则表达式更好。

const string = "56789012345" ;
string.substring(0,4)==="5678" ? true : false ;