2

Hello trying to make step by step body measurement form using jquery stepjs https://github.com/rstaib/jquery-steps/ my code submit the data on finish but need help on validation of the field that it should max 3 digit number that is the measurement before hitting the next button

<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>

My Script

   <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>

1 Answers1

1

Use regex to validate the number of digits. You will need additional onStepChanging and onFinishing parameters for steps().

The regex /^[0-9]{1,3}$/ will allow only upto 3 digits e.g. 1, 12, 123 will be allowed, but not 1234 and so on. You can write your own logic to warn user to enter max 3 digits.(e.g. red colored text)
Note: This regex will not allow blank value. To allow blank value modify regex as /^[0-9]{0,3}$/

EDIT:
You have 4 steps and every step has one input, so we need to validate the <input> which is visible on current step. So I have added current step's index(currentIndex in script) as suffix to measureinput class. You can notice measureinput0, measureinput1, measureinput2, measureinput3 classes for <input> in every <section>.

Now we can validate only current visible <input> in 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>
as-if-i-code
  • 1,362
  • 4
  • 14
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/209147/discussion-on-answer-by-as-if-i-code-how-to-validate-only-number-upto-3-digit-in). – Samuel Liew Mar 06 '20 at 10:57
  • @JunaidKhan Add different class for name and email, and validate it inside `ValidateField()` function. – as-if-i-code Mar 06 '20 at 14:21