P粉6829875772023-07-29 07:47:05
When you are not sure, you can view the output through console.log.
Please check my codesandbox. I use console.log to output step by step during the verification process, and split large steps into small steps when necessary.
For your first attempt, the problem is that Number.isFinite always returns false. This is because you passed it a string coord, but Number.isFinite expected a number. Here’s how to fix it:
Number.isFinite(parseFloat(coord))
Your initial value.split only works with commas ",". I recommend splitting on spaces, commas, and any number of spaces.
const coordinates = value.split(/[,\s]\s*/);
For your second attempt, just using regular expressions, I don't see any problems. When I put the code in the same codesandbox and use it as validation method it works perfectly.