-2

有人可以检查我的 javascript 代码,如果它们是正确的吗?我看不到电子邮件警报。我试图单击提交按钮,但在名称警报后,电子邮件无法正常工作。

function doValidate()
{
    if (document.appointment.requiredname.value =="")
    {
        alert("Please put your name");
        document.appointment.requiredname.focus();
        return false;
    }
    var readmail = document.appointment.requiredemail.value;
    var checkatsymbol = readmail.indexof("@");
    var checkdotsymbol = readmail.lastindexof(".");
    if (checkatsymbol < 1 || checkdotsymbol+2>=readmail.length )
    {
        alert("Please put the correct email address");
        document.appointment.requiredemail.focus();
        return false;
    }
    if (document.appointment.requiredphone.value =="" )
    {
        alert("Please put your phone");
        document.appointment.requiredphone.focus();
        return false;
    }
    if (document.appointment.requireddate.value =="" )
    {
        alert("Please put your appointment date as DD/MM/YYYY");
        document.appointment.requireddate.focus();
        return false;
    }
    if (document.appointment.requiredtime.value =="")
    {
        alert("Please put your appointment time as HH:MM AM/PM");
        document.appointment.requiredtime.focus();
        return false;
    }
    return ( true );
}
4

5 回答 5

0

wirte 'indexOf' 而不是 'indexof' 替换:

var checkatsymbol = readmail.indexof("@");
var checkdotsymbol = readmail.lastindexof(".");

和 :

var checkatsymbol = readmail.indexOf("@");
var checkdotsymbol = readmail.lastindexOf(".");
于 2013-10-29T07:03:48.133 回答
0

Javascript 区分大小写。

var checkatsymbol = readmail.indexof("@");
var checkdotsymbol = readmail.lastindexof(".");

应该:

var checkatsymbol = readmail.indexOf("@");
var checkdotsymbol = readmail.lastIndexOf(".");
于 2013-10-29T07:04:11.577 回答
0
var x=document.appointment.requiredemail.value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Not a valid e-mail address");
  return false;
  }

这个验证电子邮件的片段应该可以工作!!

于 2013-10-29T07:04:51.763 回答
0

您可能应该前往StackExchange 论坛CodeReview

于 2013-10-29T07:08:42.697 回答
0

您必须return false;if条件中删除才能在函数中执行以下代码

于 2013-10-29T06:59:41.630 回答