我已经尝试了 javascript 中的所有内容以获取提交按钮以在警报中显示所有输入字段。
例子。当用户填写表单时,它会进行验证,然后单击提交以在警报中显示结果,例如姓名、费用、地址和电话等。我正在尝试练习 javascript,但无法获得我想要的结果。
有人可以帮忙吗!
这是我的脚本。
<script type="text/javascript">
// Form validation code will come here.
var Num1 = document.getElementById('1stTotal').value;
var Num2 = document.getElementById('2ndTotal').value;
var Num3 = document.getElementById('3rdTotal').value;
var Total = Num1 + Num2 + Num3;
grandTotal.value = Total;
function validate() {
if (document.myForm.Name.value == "") {
alert("Please provide your name!");
document.myForm.Name.value.focus();
return false;
}
if (document.myForm.Street.value == "") {
alert("Please provide your Street!");
document.myForm.Street.value.focus();
return false;
}
if (document.myForm.City.value == "") {
alert("Please provide your City!");
document.myForm.City.focus();
return false;
}
if (document.myForm.Postal.value == "" || isNaN(document.myForm.Postal.value) || document.myForm.Postal.value.length != 7) {
alert("Please provide a Postal in the format xxx-xxx.");
document.myForm.Postal.focus();
return false;
}
if (document.myForm.Telephone.value == "" || isNaN(document.myForm.Telephone.value) || document.myForm.Telephone.value.length != 12) {
alert("Please provide a Telephone in the format xxx-xxx-xxxx.");
document.myForm.Telephone.focus();
return false;
}
var emailID = document.myForm.Email.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || (dotpos - atpos < 2)) {
alert("Please provide a valid email.");
document.myForm.Email.focus();
return false;
}
if (document.myForm.CardNo.value == "" || isNaN(document.myForm.CardNo.value) || document.myForm.CardNo.value.length != 16) {
alert("Please provide a Credit Card Number in the format 1234567890123456.");
document.myForm.CardNo.focus();
return false;
}
if (document.myForm.Expiry.value == "" || isNaN(document.myForm.Expiry.value) || document.myForm.Expiry.value.length != 10) {
alert("Please provide a Expiry in the format MM-DD-YYYY.");
document.myForm.Expiry.focus();
return false;
}
return (true);
}
function greeting() {
alert("Hello " + Name + "," + "<br />" + "Your book order includes:");
} < /script>