Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  179] [ 4]  / answers: 1 / hits: 12782  / 11 Years ago, sat, november 30, 2013, 12:00:00

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?


More From » forms

 Answers
4

First to answer your question:






Edit: Nowadays JavaScript does have a startsWith function on the String object.






JavaScript does not have a 'StartsWith' method on the String object. You will have to use regular expressions for this. Here's an example:



function validate() {
var email = document.getElementById('email').value;
var phone = document.getElementById('phone').value;

var emailFilter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
var phoneFilter = /^http:///;

if (!emailFilter.test(email)) {
alert('Please enter a valid e-mail address.');
return false;
}

if (!phoneFilter.test(phone)) {
alert('Please correct your phone number.');
return false;
}

return true;
}


I created a jsfiddle to show you how it works: http://jsfiddle.net/cnVQe/1/



However: it's best to use a declarative instead of a programmatic approach for validating forms. There are many tools that do this. One of them is this jQuery library that does most of the heavy listing for you: http://jqueryvalidation.org/



It works by just adding classes to to the fields and have the plugin do the checking , error displaying and preventing of form submission.



Just to add: you approach is not wrong, but if the form gets bigger and you want to check more fields you will inevitably run into complexity that has been solved over and over by other people. Even if it means that you pull in additional complexity with the extra plugin it will all be worth it as soon as you start to make the form bigger.



Also: your regular expression for the e-mail address check will not accept the + sign, which is something many people use to create special gmail accounts like [email protected]. Using libraries such as the one I describes very often have robust checks already. So that's an additional benefit on using existing libraries.


[#49964] Friday, November 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mitchell

Total Points: 95
Total Questions: 110
Total Answers: 87

Location: Gabon
Member since Thu, Jul 15, 2021
3 Years ago
mitchell questions
;