1

I am using jQuery validation. I am using minlength:8 and maxlength:10 for a mobile number.

Validation is working if I add 7 number

enter image description here

Note: I added 7 number with a dot(.) means 8 and it's accepted.

( If I add dot(.) at the end the I am getting the error)

enter image description here

Note: I added 9 number and it's accepted.

enter image description here

Is there any way that the user can enter eighter 8 number or 10 number?

Would you help me out with this?

$("#testForm").validate({
  rules: {
    mobileno: {
      minlength: 8,
      maxlength: 10,
      number: true
    }
  },
  submitHandler: function(form) {
    $.ajax({
      url: baseUrl + "/Test_control/submitForm",
      type: "POST",
      data: $('#testForm').serialize(),

      success: function(data) {
        window.location.href = baseUrl + "/all_thankyou";

      },
    }); // AJAX Get Jquery statment
  }
});
<form name="form" id="testForm" action="#" method="post">

  <input type="text" name="mobileno">
  <input type="submit" name="send" value="Submit">

</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/additional-methods.min.js"></script>
Sparky
  • 98,165
  • 25
  • 199
  • 285
questionbank
  • 440
  • 8
  • 25
  • Please don't do this. There is nothing worse than having a web site tell me my mobile number is wrong based on flawed validation created by some programmer who hasn't considered all current valid formats, let alone any future ones. If you want to validate a mobile number, let the user enter whatever they want and send it a text message with a response code that the user has to enter. –  Apr 27 '19 at 06:45
  • @ReddHerring, But the user should enter the correct number. This field is not required if the user enters any number then validation should check. – questionbank Apr 27 '19 at 06:52
  • My point is that you, the programmer, don't know what the correct number is. Even if you make this work your validation will fail often enough for it to be economically worthless. In the same way that you validate email by sending something to the user's address, you validate phone number by sending a message. –  Apr 27 '19 at 06:59
  • It accepted a dot because `number` rule includes decimal numbers. If you want only digits entered, then you would use the `digits` rule. Most rules/methods are explained in the documentation. – Sparky Apr 29 '19 at 18:01

2 Answers2

0

If you want to use regex then here is a regex code for matching min 8 or max 10 numbers: /(^[1-9]{8,10})/g. Test it here: https://regexr.com

Chandan
  • 217
  • 1
  • 3
  • 17
0

Using addMethod

$(document).ready(function() {
                $.validator.addMethod(
                    "mobileValidation",
                    function(value, element) {
                        return !/^\d{8}$|^\d{10}$/.test(value) ? false : true;
                    },
                    "Mobile number invalid"
                );

                $("#form").validate({
                    rules: {
                        mobile: {
                            required: true,
                            mobileValidation: $("#mobile").val(),
                        },
                    },
                });
            });
 <script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>

        <script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.0/dist/jquery.validate.min.js"></script>
        <form id="form">
            <input type="number" id="mobile" name="mobile" />
            <input type="submit" name="submit" value="submit" />
        </form>
Garach Ankur
  • 48
  • 1
  • 5
  • FYI - there are several phone number rules contained within the `additional-methods.js` file that are already part of this plugin. – Sparky Apr 29 '19 at 16:08