要在JavaScript中验证电子邮件地址,请检查“ @”和“。”的条件 即abc@gmail.com。让我们尝试以下代码来验证电子邮件
<!DOCTYPE html> <html> <body> <script> var emailID; function validateEmail(emailID) { atpos = emailID.indexOf("@"); dotpos = emailID.lastIndexOf("."); if (atpos < 1 || ( dotpos - atpos < 2 )) { document.write("Please enter correct email ID") document.myForm.EMail.focus() ; return false; } return( true ); } document.write(validateEmail("abc@gmail.com")); </script> </body> </html>