我的表单不会发送电子邮件,我认为我缺少if / elseif和return语句,但不确定在哪里插入
试图弄清楚在代码中插入return true / false语句的位置,并且代码确实为此使用了一些帮助,因为我仍然是Java脚本/ Web开发的初学者。
var message, x, j;
message = document.getElementById("message");
message.innerHTML == "";
x = document.getElementById("Firstname").value;
try {
if(x == "") throw "Required Field";
if (x = /^[a-zA-Z]*$/) throw "";
if (x = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/) throw "Invalid Characters Present";
if (x = Number(x)) throw "Invalid Characters Present";
}
catch(err) {
message.innerHTML = err;
}
}
多数民众赞成在上面的第一个功能,以验证第一个输入
function myFunction2() {
var message, x, j;
message = document.getElementById("message2");
message.innerHTML == "";
x = document.getElementById("Lastname").value;
try {
if(x == "") throw "Required Field";
if (x = /^[a-zA-Z]*$/) throw "";
if (x = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/) throw "Invalid Characters Present";
if (x = Number(x)) throw "Invalid Characters Present";
}
catch(err) {
message.innerHTML = err;
}
}
function myFunction3 (){
let message, x, j; //using let stops other functions from accidentally using it
let RegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; //pattern for email
message = document.getElementById("message3"); //getting the error element
message.innerHTML == ""; //declaring default value
x = document.getElementById("Email").value; //x equals the email value
try{ //trying the conditions
if (x === '') throw "Required Field"; //if x is blank then throw and error message
else if (RegExp.test(x)) throw ''; //if the regexp testing pattern is valid then throw null
else throw "Invalid Email Address"; //otherwise throw value is not valid
}
catch(err) { //this cathces the throw and produces the error message
message.innerHTML = err;
}
}
function myFunction4() {
var message, x, j;
message = document.getElementById("message4");
message.innerHTML == "";
x = document.getElementById("Subject").value;
try {
if(x == "") throw "Required Field";
if (x = /^[a-zA-Z]*$/) throw "";
}
catch(err) {
message.innerHTML = err;
}
}
function myFunction5() {
var message, x, j;
message = document.getElementById("message5");
message.innerHTML == "";
x = document.getElementById("Message").value;
try {
if(x == "") throw "Required Field";
if (x = /^[a-zA-Z]*$/) throw "";
}
catch(err) {
message.innerHTML = err;
}
}
答案 0 :(得分:0)
首先,我可以确定您的正则表达式是否需要正确设置语句的格式。
请参见here
例如
var phoneRE = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/;
if (phoneNo.match(phoneRE)) {
return true;
} else {
alert( "The phone number entered is invalid!" );
return false;
}