gpt4 book ai didi

javascript - 如何检查多步表单中输入值的字段?

转载 作者:行者123 更新时间:2023-12-01 04:34:08 26 4
gpt4 key购买 nike

创建了一个页面表单并检查了空值。现在如何检查用户在字段中输入的内容?

Step 1. First Name and Last Name = check (more than 2 characters, only letters without spaces and without “-”, “/”)

Step 2. Number 1 and Number 2 = digits only

Step 3. City and Country = letters only

Step 4. Email, phone and date = (I made masks on the date and phone), email of this format (email@gmail.com)

这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
<script src="jquery.maskedinput.js"></script>
<style>
#step2,#step3,#step4,#step5{
display: none;
}
</style>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(e){
var current = 0;
$("#Phone").mask("(999) 999-9999");
$("#date").mask("99.99.9999",{placeholder:"dd.mm.yyyy"});
$.validator.addMethod("pageRequired", function(value, element) {
var $element = $(element)
function match(index) {
return current == index && $(element).parents("#step" + (index + 1)).length;
}
if (match(0) || match(1) || match(2) || match(3)) {
return !this.optional(element);
}
return "dependency-mismatch";
},$.validator.messages.required);
var v = $("#cmaForm").validate({
errorClass: "warning",
onkeyup: false,
onfocusout: false,
submitHandler: function() {
alert("Submitted, thanks!");
}
});
$(".next1").click(function() {
if (v.form()) {
$("#step2").show();
$("#step1").hide();
current = 1;
$("#progressText").html("Step 2 of 4");
}
});
$(".next2").click(function() {
if (v.form()) {
$("#step3").show();
$("#step2").hide();
current = 2;
$("#progressText").html("Step 3 of 4");
}
});
$(".next3").click(function() {
if (v.form()) {
$("#step4").show();
$("#step3").hide();
current = 3;
$("#progressText").html("Step 4 of 4");
}
});

});
</script>
<div id="progressText">Step 1 of 4</div>
<form id="cmaForm" action="" method="post">
<ul id="stepForm">
<li id="step1">
<p>
<label>First name:</label>
<input type="text" name="fname" class="pageRequired"></p>
<p>
<label>Last name:</label>
<input type="text" name="lname" class="pageRequired"></p>
<p class="buttonWrapper">
<input name="formNext1" type="button" class="next1 nextbutton" value="Next" alt="Next" title="Next">
</p>
</li>
<li id="step2">
<p>
<label>Number1:</label>
<input type="text" name="num1" class="pageRequired" title="Enter number 1"></p>
<p>
<label>Number2:</label>
<input type="text" name="num2" class="pageRequired" title="Enter number 2"></p>
<p class="buttonWrapper">
<input name="formNext1" type="button" class="next2 nextbutton" value="Next" alt="Next" title="Next">
</p>
</li>
<li id="step3">
<p>
<label>City:</label>
<input type="text" name="city" class="pageRequired"></p>
<p>
<label>Country:</label>
<input type="text" name="country" class="pageRequired"></p>
<p class="buttonWrapper">
<input name="formNext1" type="button" class="next3 nextbutton" value="Next" alt="Next" title="Next">
</p>
</li>
<li id="step4">
<p><label>Email Address:</label><input name="email" id="email" class="pageRequired" title="Email address is required"></p>
<p><label>Phone Number:</label><input name="Phone" id="Phone" class="pageRequired" maxlength="254" title="Phone Number is required"></p>
<p><label>Date:</label>
<input type="text" name="date" id="date" class="sum pageRequired" title="Date is required"></p>
<input type="submit" class="submitbutton" value="Submit">
</li>
</ul>
</form>
</html>
</body>

最佳答案

尝试使用

$("#step2").css("visibility", "hidden");
$("#step3").css("visibility", "visible");

而不是

$("#step2").hide()
$("#step3").show();

为您的输入提供唯一的 ID

<p>
<label>Number1:</label>
<input type="text" name="num1" id="num1" class="pageRequired" title="Enter number 1"></p>

这样你仍然可以读取输入值。

$("#num1").val();

但是您也可以使用普通表单提交来接收所有值。

关于javascript - 如何检查多步表单中输入值的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60071026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com