gpt4 book ai didi

jquery - 如何使用 jquery-step 在每个步骤中仅验证最多 3 位数字

转载 作者:行者123 更新时间:2023-12-01 04:33:40 25 4
gpt4 key购买 nike

您好,尝试使用jquery stepjs制作一步一步的 body 测量表格https://github.com/rstaib/jquery-steps/我的代码完成后提交数据,但需要验证字段的帮助,该字段最多应为 3 位数字,即点击下一个按钮之前的测量值

<form id="form" name="form" method="post" action="mail/men-measure.php" class="measureinput-inner">

            <section class="tabs-continners">
<input type="text" class="measureinput" placeholder="Enter Measurement In cm" id="bandhgala-length" name="bandhgala-length">
</section>

<section class="tabs-continners">
<input type="text" class="measureinput" placeholder="Enter Measurement in cm" id="bundi-length" name="bundi-length">
</section>

<section class="tabs-continners">
<input type="text" class="measureinput" placeholder="Enter Measurement In cm" id="fly" name="fly" >
</section>
</div>
</div>

我的脚本

   <script src="js/jquery.steps.js"></script>

<script>
$(function ()
{
$("#wizard").steps({
headerTag: "h2",
bodyTag: "section",
transitionEffect: "slideLeft",
stepsOrientation: "vertical",
onFinished: function (event, currentIndex) {
$("#form")[0].submit();
}
});

});
</script>

最佳答案

使用正则表达式验证位数。您将需要额外的onStepChangingonFinishing steps() 的参数。

正则表达式 /^[0-9]{1,3}$/最多只允许 3 位数字,例如允许使用 1、12、123,但不允许使用 1234 等。您可以编写自己的逻辑来警告用户最多输入 3 位数字。(例如红色文本)
注意:此正则表达式不允许空白值。要允许空白值,请将正则表达式修改为 /^[0-9]{0,3}$/

编辑:
您有 4 个步骤,每个步骤都有一个输入,因此我们需要验证 <input>这在当前步骤中可见。因此,我将当前步骤的索引(脚本中的currentIndex)作为后缀添加到measureinput类(class)。您可以注意到measureinput0, measureinput1, measureinput2, measureinput3 <input> 的类(class)在每个 <section> .

现在我们只能验证当前可见的 <input>在 JavaScript 中。

$(function() {
$("#wizard").steps({
headerTag: "h2",
bodyTag: "section",
transitionEffect: "slideLeft",
stepsOrientation: "vertical",
enableKeyNavigation: true,

onStepChanging: function(event, currentIndex, newIndex) {

// always clear error message on click of 'next' and 'previous'
clearErrorMessage('measureinput' + currentIndex);

// in case of 'next', newIndex is greater than currentIndex
// so below condition will validate only in case of 'next', not 'previous'
if (currentIndex < newIndex) {
return ValidateField('measureinput' + currentIndex);
}
else {
return true;
}
},

onFinishing: function(event, currentIndex) {
return ValidateField('measureinput' + currentIndex);
},
onFinished: function(event, currentIndex) {
$("#form")[0].submit();
}
});

function ValidateField(classNameOfField) {
var allFields = $("." + classNameOfField);
for (i = 0; i < allFields.length; i++) {
if (/^[0-9]{1,3}$/.test(allFields[i].value)) {
return true;
} else {
//alert('Max 3 digits are allowed!'); // you can write your own logic to warn users
showErrorMessage(classNameOfField);
return false;
}
}
}

function showErrorMessage(classNameOfField)
{
$("."+classNameOfField).css("border-color", "red");
$("#errorMessage").css("display", "block");
}

function clearErrorMessage(classNameOfField)
{
$("."+classNameOfField).css("border-color", "black");
$("#errorMessage").css("display", "none");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-steps/1.1.0/jquery.steps.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<form id="form" name="form" method="post" action="mail/men-measure.php" class="measureinput-inner">
<div class="content">

<div id="wizard">
<h2>Personal Detail</h2>
<section class="tabs-continners">
<input type="name" class="measureinput0" placeholder="Enter Name" id="acrossfront" name="across-front">
<input type="email" class="measureinput0" placeholder="Enter Email" id="acrossfront" name="email">
</section>
<h2>Across Front</h2>
<section class="tabs-continners">
<input type="text" class="measureinput0" placeholder="Enter Measurement In cm" id="acrossfront" name="across-front">
</section>

<h2>Across Back</h2>
<section class="tabs-continners">
<input type="text" class="measureinput1" placeholder="Enter Measurement in cm" id="across-back" name="across-back">
</section>

<h2>Bundi Length</h2>
<section class="tabs-continners">
<input type="text" class="measureinput2" placeholder="Enter Measurement in cm" id="bundi-length" name="bundi-length">
</section>

<h2>Bandhgala Length</h2>
<section class="tabs-continners">
<input type="text" class="measureinput3" placeholder="Enter Measurement In cm" id="bandhgala-length" name="bandhgala-length" required>
</section>
<span id="errorMessage" style="display:none;color:red;"> Maximum 3 digits are allowed</span>
</div>
</div>
</form>

关于jquery - 如何使用 jquery-step 在每个步骤中仅验证最多 3 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60507677/

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