I'm having an issue with my JavaScript code that is supposed to validate a phone number field, but it doesn't seem to be working. Even if I enter incorrect values, the form still submits. Here's the snippet of my code:
<script>
function validatePhone()
{
var num1 = document.getElementById('workno');
if (num1 !== null)
{
regex = /\(\d{2}\)\d{8}/;
}
if (!num1.match(regex))
{
alert('That is not a correct telephone number format');
return false;
}
}
</script>
<form name="eoiform" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return validatePhone();">
<input type="text" id="workno" name="workno">
<input type="submit" name="submit" id="submit" value="submit">
</form>
Could someone help me pinpoint where I might have made a mistake?