Ad

What Is The Right Regex To Allow Blank Or Numeric Values

I want to validate one variable named "Port" in my JavaScript to accept only either numerical digits or a blank value. I am not sure exactly what regex I can use to satisfy both conditions. Can anyone suggest me logic to use under my if loop to validate this?

Ad

Answer

If you want to achieve your goal with regexes, you are looking for:

const regex = /^\d*$/;

console.log(regex.test("")); // true
console.log(regex.test("8080")); // true
console.log(regex.test("4d54")); // false

Ad
source: stackoverflow.com
Ad