Search code examples
node.jsexpressnode-modulesexpress-validatornodejs-stream

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


I need to validate the first four digits in a phone number which should be "5678" in nodeJs how should I do it. Which validator library should I use


Solution

  • It is best practice to depends on external library only if you need it, in my opinion.

    In your case, you can check if your phone number start with "5678" with a very simple regular expression:

    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
    

    ^ stand for matches beginning of input

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