0

I am using the jQuery.validator.addClassRules method to validate my input fields. How do I allow my textbox to accept only comma seperated values and show the default message if incorrect.

<input type="text" class="form-control numberValidation" />


<!-- JS Plugins Init. -->
<script>
    $(document).on('ready',
        function () {

            $("#signupform").validate({

            errorClass: 'invalid-input'            

        });

        jQuery.validator.addClassRules('numberValidation',
        {
            number: true
        });
    });
</script>
SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
Zain Nabi
  • 43
  • 6
  • 2
    Please share with us an example with valid and invalid input values – adampweb Dec 15 '20 at 14:32
  • What inputs would you be considering as invalid? You wouldn't want to invalidate if it doesn't have any commas because if you only had one item, it shouldn't have a comma at the end. – Dan Csharpster Dec 15 '20 at 15:32
  • 1
    If you wrapped each item in single or double quotes, you could validate that the character inbetween, if there is more than one item, has to be a comma – Dan Csharpster Dec 15 '20 at 15:38
  • @Dan Csharpster example 0,00 – Zain Nabi Dec 16 '20 at 08:52
  • @ZainNabi, validation is more about what you're going to disallow. You can validate something is a valid date, but often you're validating that its not a negative number, etc. What is somebody tries to send strings with commas and then tries to separate them with semicolons, like: foo,bar;123,,,456;abc;def ? Is that invalid input? I would argue that its fine, it is valid input and just parse it on the commas and let the user see their bad input. Maybe have a preview step before saving the values? – Dan Csharpster Dec 16 '20 at 14:00
  • @Dan Csharpster way off topic. – Zain Nabi Dec 16 '20 at 16:25
  • Its actually not. What are you considering invalid values? – Dan Csharpster Dec 16 '20 at 18:15
  • @Dan Csharpster strings are not allowed. Only comma separated numbers are allowed e.g. 0,00 , 1,20 – Zain Nabi Dec 17 '20 at 02:55

1 Answers1

0

If you combine this answer which shows you how to validate with a regular expression with this answer that contains regex examples for comma separated integers, that should meet your needs.

Dan Csharpster
  • 2,662
  • 1
  • 26
  • 50