您好尝试使用jquery stepjs逐步制作身体测量表格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>
使用正则表达式验证数字位数。您将需要额外的onStepChanging
和onFinishing
参数steps()
。
正则表达式/^[0-9]{1,3}$/
最多只能输入3位数字,例如1、12、123,但不允许1234,依此类推。您可以编写自己的逻辑来警告用户输入最多3位数字。(例如,红色文本)
注意:此正则表达式不允许空白值。要允许空白值,将正则表达式修改为/^[0-9]{0,3}$/
编辑:
您有4个步骤,每个步骤都有一个输入,因此我们需要验证<input>
在当前步骤上可见的。因此,我在类中添加了当前步骤的index(currentIndex
在脚本中)作为后缀measureinput
。您可以在每个中注意到的measureinput0, measureinput1, measureinput2, measureinput3
类。<input>
<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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句