我在顶部添加了此验证电子邮件功能,我不知道如何将其作为参考。元素[6]是表单中的电子邮件字段,我不知道
是什么return re.test(email);
声明就是这样 这是完整的脚本代码:
<script type="text/javascript">
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function validation(form) {
if(form.elements[3].value == '' ) {
alert('Please enter your first name');
form.first_name.focus();
return false;
}
if(form.elements[4].value == '') {
alert('Please enter your last name');
form.last_name.focus();
return false;
}
if(form.elements[6].value == '') {
alert('Please enter your email address');
form.email.focus();
return false;
if (validateEmail(email)) {
$("#result").text(email + " is a valid email address :)");
$("#result").css("color", "green");
} else {
$("#result").text(email + "is not a valid email address :(");
$("#result").css("color", "red");
return false;
}
if(form.elements[7].value == '') {
alert('Please enter your telephone number');
form.phone.focus();
return false;
}
if(form.elements[17].value == '') {
alert('Please let us know what you are most interested in');
form.phone.focus();
return false;
}
if(form.elements[18].value == '') {
alert('Please enter the area you are most interested in');
form.phone.focus();
return false;
}
if(form.elements[20].value == '') {
alert('Please enter the low end of your budget');
form.phone.focus();
return false;
}
if(form.elements[21].value == '') {
alert('Please enter the high end of your budget');
form.phone.focus();
return false;
}
return true;
}
</script>
答案 0 :(得分:1)
功能:
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
有一个正则表达式对象(re
),正则表达式对象使用test
函数来测试传入函数的电子邮件是否根据正则表达式进行验证。如果test
函数验证为电子邮件,则true
函数会返回false
,如果不是,则if(!validateEmail(form.elements[6].value)) {
alert('Please enter a valid email address');
form.email.focus();
return false;
}
会返回。{/ p>
你可以像这样使用它:
{{1}}
测试函数是regex对象的原生函数,这意味着您的浏览器将知道如何处理它(测试函数的规范:developer.mozilla.org/en/JavaScript/Reference/Global_Objects/)
答案 1 :(得分:0)
传递给validation()
的{{1}}函数变量email
内部几乎未定义。
你应该:
validateEmail
OR
validateEmail(form.email.value);
希望这会有所帮助......
答案 2 :(得分:0)
form.elements
是您在1996年选择DOM元素的方式。您应该使用document.getElementById
代替:
var resultElement = $("#result");
var emailVal = document.getElementById('emailBoxId').value;
if (validateEmail(emailVal )) {
resultElement
.text(emailVal + " is a valid email address :)")
.css("color", "green");
} else {
resultElement
.text(emailVal + "is not a valid email address :(")
.css("color", "red");
return false;
}
答案 3 :(得分:0)
创建变量email
并将其值设置为form.elements[6].value
e.g。
function validation(form) {
if(form.elements[3].value == '' ) {
alert('Please enter your first name');
form.first_name.focus();
return false;
}
//...
var email = form.elements[6].value
if (validateEmail(email)) {
$("#result").text(email + " is a valid email address :)");
$("#result").css("color", "green");
} else {
$("#result").text(email + "is not a valid email address :(");
$("#result").css("color", "red");
return false;
}
//...
}