我目前正在开发一个项目,要求我在包含条件输入字段的表单上进行javascript表单验证(访问者可以选择是通过用户号码还是电子邮件地址登录)。两个输入字段都是分开的,我需要执行以下操作:
如果访问者选择通过选项A(用户编号)登录,则验证应仅考虑输入字段A(用户编号)的ID,而不需要验证其他字段(电子邮件地址)。
反之亦然,如果访问者选择选项B。
我目前用于验证的代码:
function empty() {
var x;
x = document.getElementById("user_number").value;
if (x == "") {
MsgBox('Your user number is required.', 'ERROR');
return false;
}
var y;
y = document.getElementById("email").value;
if (y == "") {
MsgBox('Your email address is required.', 'ERROR');
return false;
}
}
表格触发事件:
<form method="POST" id="accordion-top-form" action="" onsubmit="return empty();">
我需要展开当前脚本以检查在提交表单时是否填写了字段A或字段B(然后自动禁用其他字段的验证)。
我该怎么做?
答案 0 :(得分:1)
听起来这样就够了吗?
我个人不会将该函数称为空,因为您要返回true以允许提交
function empty() {
var x = document.getElementById("user_number").value,
y = document.getElementById("email").value;
x = x?x.trim()|| ""; // handle null and all blanks
y = y?y.trim()|| "";
if (x === "" && y === "") {
alert("Please enter user number or email")
return false;
}
// optional
if (x && y) { // both entered
alert("Please enter EITHER user number or email")
return false;
}
if (x) return isValidUser(x); // each of these functions needs to return boolean
if (y) return isValidEmail(y);
// likely not going to happen
return false;
}
答案 1 :(得分:1)
您可以使用以下内容:
mysql_query("CREATE TABLE IF NOT EXISTS `test_q` (`code` varchar(30) NOT NULL,
`va_desc` text NOT NULL,
`price` text NOT NULL,
`category` text NOT NULL,
PRIMARY KEY (`code`),
);
mysql_query("insert into `test_q` (code, va_desc, price, category)
values ('$code', '$desc', '$price', '$categ')
on duplicate key update va_desc='$desc', price='$price', category='$categ'");
并更改您的表单,以便内联var forms = {
user: 0,
email: 1
};
function whichForm() {
var userForm = document.getElementById("user_number").value;
var emailForm = document.getElementById("email").value;
if (userForm && emailForm) {
//user wrote in both forms, something is wrong
} else if (!userForm && !emailForm) {
//user didn't fill in any form
} else {
return userForm ? forms.user : forms.email;
}
}
function empty(form) {
if (form === forms.user) {
// check if the user number form is empty
var userForm = document.getElementById("user_number").value;
if(userForm.trim() === "") {
// possibly do more validation
// return true or false based on whether you want to submit
}
} else if (form === forms.email) {
// check if the email form is empty
var emailForm = document.getElementById("email").value;
if(emailForm.trim() === "") {
// possibly do more validation
// return true or false based on whether you want to submit
}
} else {
// something is wrong, invalid parameter,
// handle here
return false
}
}
function validate() {
return empty(whichForm());
}
或仅return validate()
作为提交处理程序。
答案 2 :(得分:0)
您可以测试两者是否为空
function empty() {
var a = document.getElementById("user_number").value,
b = document.getElementById("email").value;
if ("" == a && "" == b) return MsgBox("Your user number or mibile is required.", "ERROR"), !1
};
答案 3 :(得分:0)
代码以这种方式执行:
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.sql._
import org.apache.spark.sql.SQLContext
import org.apache.spark.SparkConf
object SparkPGSQLConnect {
case class Projects(ID:Int, Name:String, Address:String)
def main(args:Array[String]) {
val conf = new SparkConf().setMaster(“local[*]”).setAppName("PostGreSQLConnection")
//val conf = new SparkConf().setMaster("yarn-cluster").setAppName("PostGreSQLConnection")
val sc = new SparkContext(conf)
val sqlContext= new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
val jdbcDF = sqlContext.load("jdbc", Map(
"url" -> "jdbc:postgresql:tempdb?user=******&password=******”,
"dbtable" -> “employee”))
jdbcDF.show(false)
}
}
答案 4 :(得分:0)
提议的解决方案: 检查两个输入字段中的哪一个填满:
var inputA = document.getElementById('A').value;
var inputB = document.getElementById('B').value;
if ((inputA !== "") || (inputA !== NaN) || (inputA !== undefined)) {
//execute code for the user number
}
else if ((inputB !== "") || (inputB !== NaN) || (inputB !== undefined)) {
//execute code for the email
}
else {
//display an error saying none of the two fields were used
}
推荐:大多数网站只使用1个输入,因为它看起来更清晰。占位符文本可以向用户指定他应该输入的输入选项:
<input type="text" id="input1" placeholder="user number or email">
建议的解决方案:检查用户输入是否有@
符号:
var input1 = document.getElementById("input1").value;
var emailInput = input1.includes('@');//returns a boolean with a value of true
//if '@' was found in the string input1
if (emailInput) {
//execute the code for the email input
} else {
//execute the code for the userID input
}
<强>解释强>
我认为您想在<form ...>
标记内使用相同的输入字段,无论用户是使用电子邮件还是ID号登录。从中我认为最符合逻辑的就是找到其中一个输入所特有的内容,并根据所提供的输入中是否存在唯一元素来建立代码逻辑。
AKA,因为电子邮件始终具有@
符号,验证提供的字符串中是否存在此符号应该足以验证用户是否使用电子邮件或ID号尝试登录。
如果有帮助,请告诉我:)。