I am a total JavaScript noob, but I have been searching for a solution for quite some time now and can't seem to find it.
What I want to do is: Before sending a form, check if the entered e-mail address is valid and also check if the input of the phone field starts with http://.
Validating the e-mail address is working like a charm. However, the second part isn't. The form is just sent without any alert or anything.
Can someone enlighten me? Thank you!
function validate(){ var email = document.getElementById('email').value; var phone = document.getElementById('phone').value; var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/; if (!filter.test(email)) { alert('Please enter a valid e-mail address.'); return false; } else if (phone.StartsWith("http://")) { alert('Please correct your phone number.'); return false; } } For anyone wondering: this is supposed to be a "simple" spam blocker.
Maybe it would be even more interesting if the phone field was checked for any characters except numbers, plus, spaces, hyphens and dots...
What do you think?