1👍
When in doubt, console.log it out
See my codesandbox where I console.log each step of your validation, splitting big steps into smaller steps where necessary.
For your first attempt, the problem is Number.isFinite always returning false. This is because you feed it a string coord
but Number.isFinite
expects a number. The fix is then:
Number.isFinite(parseFloat(coord))
Your initial value.split
also only works with comma ,
. I suggest splitting on space
, comma ,
and comma + any amount of space ,
const coordinates = value.split(/[,\s]\s*/);
For your second attempt using just regex, I see nothing wrong. I put the code in the same codesandbox and when used as the validation method it works perfectly.
Source:stackexchange.com