如何使用 jquery-step 在每个步骤中仅验证最多 3 位数字
How to Validate only number upto 3 digit in each Step using jquery-step
你好尝试使用 jquery stepjs https://github.com/rstaib/jquery-steps/ 逐步制作 body 测量表格
我的代码在完成时提交了数据,但需要帮助验证该字段,它应该最多 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>
使用正则表达式验证位数。 steps()
需要额外的 onStepChanging
和 onFinishing
参数。
正则表达式 /^[0-9]{1,3}$/
最多只允许 3 位数字,例如允许使用 1、12、123,但不允许使用 1234 等等。您可以编写自己的逻辑来警告用户最多输入 3 位数字。(例如红色文本)
注意:此正则表达式不允许空值。要允许空白值将正则表达式修改为 /^[0-9]{0,3}$/
编辑:
您有 4 个步骤,每个步骤都有一个输入,因此我们需要验证在当前步骤中可见的 <input>
。所以我添加了当前步骤的索引(脚本中的 currentIndex
)作为 measureinput
class 的后缀。您可以注意到每个 <section>
.
中 <input>
的 measureinput0, measureinput1, measureinput2, measureinput3
classes
现在我们只能验证 JavaScript 中当前可见的 <input>
。
$(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 stepjs https://github.com/rstaib/jquery-steps/ 逐步制作 body 测量表格 我的代码在完成时提交了数据,但需要帮助验证该字段,它应该最多 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>
使用正则表达式验证位数。 steps()
需要额外的 onStepChanging
和 onFinishing
参数。
正则表达式 /^[0-9]{1,3}$/
最多只允许 3 位数字,例如允许使用 1、12、123,但不允许使用 1234 等等。您可以编写自己的逻辑来警告用户最多输入 3 位数字。(例如红色文本)
注意:此正则表达式不允许空值。要允许空白值将正则表达式修改为 /^[0-9]{0,3}$/
编辑:
您有 4 个步骤,每个步骤都有一个输入,因此我们需要验证在当前步骤中可见的 <input>
。所以我添加了当前步骤的索引(脚本中的 currentIndex
)作为 measureinput
class 的后缀。您可以注意到每个 <section>
.
<input>
的 measureinput0, measureinput1, measureinput2, measureinput3
classes
现在我们只能验证 JavaScript 中当前可见的 <input>
。
$(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>