Mr.Happy
Mr.Happy

Reputation: 2647

jQuery validation - Custom regex to check comma separated value

I have bellow html form which has only filed and submit button. Field name is names which is holding comma separated value like this Salvis,Sumeet,Jacob,Srlawr,Jack,.

HTML Form

<form id="frmUsers" method="post" action="#">
    <div>
    <label for="names">Names:</label>
    <input type="text" name="names" id="names" value="Salvis,Sumeet,Jacob,Srlawr,">
    </div>
    <div>
    <input type="submit" name="submit" value="Submit">
    </div>
</form>

I am using jQuery validation for validating form. Now I have also created custom regex but I don't know how to create regex for my example.

Now I want to validate form like this condition:

   if comma value is more then 3 like (Salvis,Sumeet,Jacob,Srlawr,) display error
   else don't display error.

jQuery Code

$(window).load(function(){
    // Names Custom Regex
    $.validator.addMethod("NamesRegex", function(value, element) {
        return this.optional(element) || /^[a-zA-Z ]{2,500}$/i.test(value);
    }, "Required field");

    $("#frmUsers").validate({
    rules: {
        names: {
        required: true,
        NamesRegex: true
        }
    },
    messages: {
        "names": {
        required: "Required field"
        }
    }
    });
});

What is valid result and not valid result:

Valid: Salvis,Sumeet,Jacob, 
Invalid: Salvis,Sumeet,Jacob,Srlawr,

Note: Please don't ignore last comma in regex which is important me.

Any Idea How to create regex for this way.

Thanks.

Upvotes: 1

Views: 4059

Answers (6)

Avinash Raj
Avinash Raj

Reputation: 174696

You could try the below regex,

^(?:[A-Z][a-z]+,){1,3}$

DEMO

Explanation:

^                        the beginning of the string
(?:                      group, but do not capture (between 1 and 3
                         times):
  [A-Z]                    any character of: 'A' to 'Z'
  [a-z]+                   any character of: 'a' to 'z' (1 or more
                           times)
  ,                        ','
){1,3}                   end of grouping
$                        before an optional \n, and the end of the
                         string

Upvotes: 1

Roshan
Roshan

Reputation: 2184

Using regex you can do in this way

var string="Salvis,Sumeet,Jacob,Srlawrjhkjh"

if(string.match(/,/g).length >3)
    alert("Incorrect")
   else
      alert("correct")

Upvotes: 0

Ben
Ben

Reputation: 1143

All the current solutions use split or RegExp, but it's fastest to simply count the commas like so:

commas = 0;
for (var i = 0; i < value.length; i++) {
    if (value[i] === ',') commas++;
    if (commas > 3) return false;
}
return true;

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

If the values are comma delimited, you could convert it to an array and test the length:

$.validator.addMethod("NamesRegex", function(value, element) {
    return this.optional(element) || value.split(',').length <= 3;
}, "Required field");

You could also extend this to remove trailing commas if needed.

Upvotes: 0

trainoasis
trainoasis

Reputation: 6720

If you only want to count the commas in your string you could use

 <YourStringVar>.split(",").length - 1 

to get number of commas and then validate depending on enough commas.

Upvotes: 0

Edi G.
Edi G.

Reputation: 2422

You can check it with the split function

var count = $('#names').val().split(',').length

Upvotes: 0

Related Questions