I'm trying to validate a phone number. This doesn't seem to work though. Why doesn't this work?
<html>
<head>
<script>
function validateForm() {
var phone1 = document.forms["myForm"]["phone"].value;
var phone2 = ^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$;
if ( phone1.value.match(phone2) ){
return true;
}else{
alert("Please enter a valid phone number");
return false;
}
}
</script>
</head>
<body>
<form name="myForm"
onsubmit="return validateForm()" method="post">
<label for="phone">Phone:</label><input name="phone" id="phone" value="" placeholder="Zip..." type="text" onsubmit="return validateForm()"/>
<button type="submit">Submit</button>
</form>
</body>
</html>
It definitely has something to do with the value comparison in the if statement, I just can't figure out how to accurately do this.