How 'bout:
if (/^(?:012|013|019|014|016)\d{7}$/.test(str)) {
// okay
}
That's a regular expression (MDC has a good page on them) which looks for an alternation (any of 012, 013, 019, 014, or 016) followed by exactly seven digits. The ^
at the start matches the start of the string, and the $
at the end matches the end, disallowing extraneous characters. You might consider trimming whitespace before doing this.
I used an alternation rather than ^01[23946]
just for clarity and to allow for other prefixes being easily added.
Live test
Side note: I always try to discourage people from validating phone numbers. It just ends up irritating people when they need (need) to input something your rules haven't allowed for. Also, usually 10-digit phone numbers are written in groups, e.g. here in the UK, "01234 567 890" (sometimes written "01234 56 78 90") or "020 8123 4567" (sometimes written "0208 123 4567"). You'd have to strip out internal spaces before validating to allow people to write them in the various ways people do.