[英]JavaScript Validation Registration Form
The validateForm function works. validateForm 函数有效。 And the validateEmail function works as well but separately.
并且 validateEmail 函数也可以运行,但需要单独运行。 How do I incorporate the data validation for the email in the validateForm function?
如何在 validateForm 函数中合并电子邮件的数据验证? In other words, how do I get the form to return false for when the fields are empty and requirements are not met?
换句话说,当字段为空且不满足要求时,如何让表单返回 false?
function validateForm() {
if( document.myForm.userName.value == "" ) {
alert( "Please provide your name!" );
document.myForm.userName.focus() ;
return false;
}
if( document.myForm.email.value == "" ) {
alert( "Please provide your Email!" );
document.myForm.email.focus() ;
return false;
}
return( true );
}
function validateEmail() {
var emailID = document.myForm.email.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 )) {
alert("Please enter correct email ID")
document.myForm.email.focus() ;
return false;
}
return( true );
}
You just have to merge your code into one function您只需要将代码合并到一个函数中
function validateForm() {
var emailID = document.myForm.email.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if( document.myForm.userName.value == "" ) {
alert( "Please provide your name!" );
document.myForm.userName.focus() ;
return false;
}
if( document.myForm.email.value == "" ) {
alert( "Please provide your Email!" );
document.myForm.email.focus() ;
return false;
}
if (atpos < 1 || ( dotpos - atpos < 2 )) {
alert("Please enter correct email ID")
document.myForm.email.focus() ;
return false;
}
return true;
}
just called validateEmail inside validateForm like this :刚刚在 validateForm 中调用了 validateEmail ,如下所示:
function validateForm() {
if( document.myForm.userName.value == "" ) {
alert( "Please provide your name!" );
document.myForm.userName.focus() ;
return false;
}
if( document.myForm.email.value == "" ) {
alert( "Please provide your Email!" );
document.myForm.email.focus() ;
return false;
}
return validateEmail();
}
function validateEmail() {
var emailID = document.myForm.email.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 )) {
alert("Please enter correct email ID")
document.myForm.email.focus() ;
return false;
}
return( true );
}
or u can u merge those function into like answer from @Addis或者你可以将这些函数合并成@Addis的类似答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.