0

Using jQuery validation plugin but it has no CSV validation. I have made an additional validation for this but can't get the RegEx right.

Here is what I have:

jQuery.validator.addMethod("csv", function(value, element) {
  return this.optional(element) || /([\w.$]+?(,[\w.]+)+)/.test(value);
}, "Must be comma separated if entering multiple values: value1,value2");

Any thoughts for the RegEx?

([\w.$]+?(,[\w.]+)+)

What I'm trying to do is have the user enter in a value, which can be a single value or multiple values in CSV format.

Example 1:

value1

Example 2:

value1,value2,etc... no limit

4
  • Can you provide an example of invalid input? If nothing else is defined, every string in the world is a comma separated list. Commented Apr 22, 2009 at 18:17
  • Valid input = a-z A-Z 0-9 could also include - _ Commented Apr 22, 2009 at 19:19
  • Does your CSV input allow values that have commas in them? Because that drastically complicates the regular expression. If so, will you allow values to be double-quoted to fix this? And then, if it does and you're using double quotes, are there valid values that have double quotes in them? :) I believe the CSV definition says that double quotes are escaped like """. Of course, none of this matters if your values can't have commas. Commented Apr 22, 2009 at 21:57
  • A to Z upper and lower case, Numeric 9-0, Hyphens - and underscores _ no other values should be allowed. They will all be separated by comma , but there could be one only value with no comma or multiple values with no limit. Commented Apr 23, 2009 at 12:44

2 Answers 2

4

Building on Chad's example, just switch [^,] to \w if you want only word characters. If you actually want just letters and digits, you'll need something like [a-z0-9] as \w includes the underscore character.

Based on your comment, see if

/^([a-z0-9])+(,[a-z0-9]+)*$/

does the trick.

Sign up to request clarification or add additional context in comments.

1 Comment

works great for the first value but doesn't validate the second value Example Value: 1,! <-- Should be invalid, but jQuery treats it as valid. Any thoughts?
0

Maybe I'm not totally understanding what you need, but it seems like you could capture all the values with this regex:

/([^,]+),?/

This will allow spaces and special characters in the values, I'm not sure if you want that or not, since your attempt seemed to be trying to only allow word characters.

If you are just trying to check whether they entered something valid or not though, I'm not sure what you would want to consider "invalid". It seems to me that just about anything would be valid. If it doesn't have any commas, it's a single value. If it does, it's multiple values. Maybe if you give some more detail about what data you're expecting I can write a better validating expression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.