admin管理员组文章数量:1317898
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?
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?
Share Improve this question edited Nov 30, 2013 at 14:18 KuronosuKami asked Nov 30, 2013 at 14:12 KuronosuKamiKuronosuKami 631 gold badge1 silver badge9 bronze badges2 Answers
Reset to default 4First 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/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/
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 plexity that has been solved over and over by other people. Even if it means that you pull in additional plexity 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.
You need a phone number checker function to be sure that the number is correct. Here it goes : http://www.w3resource./javascript/form/phone-no-validation.php
本文标签:
版权声明:本文标题:JavaScript: Form: Validating e-mail address and checking another field with Starts.With before sending - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742034059a2416999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论