想用一个 javascript 函数验证 html 表单。我有表单验证,仅验证该字段是否为空。我想检查电子邮件是否正确以及电话号码是否为数字。现在表单打开弹出警报,但在输入电子邮件后它会停止。
这是我的表单代码
<form name="appointment" method="post" onsubmit="return doValidate();">
Name:*<br />
<input type="text" name="requiredname" />
Email:*<br />
<input type="text" name="requiredemail" />
Phone:*<br />
<input type="text" name="requiredphone" />
Appointment Date:*<br />
<input type="text" name="requireddate" />
Appointment Time:*<br />
<input type="text" name="requiredtime" /><br /><br />
<input name="submit" type="submit" value="Book Now" /> </form>
这是 Javascript 代码:-
function doValidate()
{
if (document.appointment.requiredname.value =="")
{
alert("Please put your name");
document.appointment.requiredname.focus();
return false;
}
var readmail = document.appointment.requiredemail;
var checkatsymbol = readmail.indexof("@");
var checkdotsymbol = readmail.lastindexof(".");
if (readmail.value =="" || checkatsymbol<1)
{
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 );
}