validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

How to validate full name?

Open oppytut opened this issue 9 years ago • 3 comments

full name contains a space only in the middle

oppytut avatar Nov 18 '17 08:11 oppytut

@feira While I don't know of a way with this library, (Maybe open a PR to add that validation) you can do something like:

let fullName = "John Gill" return (fullName ? fullName.split(" ").length : 0) === 2; console.log(check === 2);

This will take care of double spaces leading and trailing spaces as well. You will have issues when it comes to "John Gill III" or other weird names that may be spaced or have Jr.'s, ect. You Can add a dropdown for these people to select from like many sites do.

TallOrderDev avatar Nov 20 '17 23:11 TallOrderDev

How about adding this as a new validator in the package? This is must needed validation for any web project.

vishalshettydev avatar Jun 25 '18 03:06 vishalshettydev

I came with this solution and it validates the full name of first name and last name.

validateFullName = (name) => {
        const fullName = name.split(" ");
        let isValid = false;

        fullName.length <=2 ? (
            isValid = validator.isAlpha(fullName[0]) && validator.isLength(fullName[0],{min:3,max:30}),
            fullName[1] ? isValid = validator.isAlpha(fullName[1]) : ''
        ) : '';

        return isValid;
    }

vishalshettydev avatar Jun 25 '18 04:06 vishalshettydev