I’d like to allow a user to be able to enter a list of IDs into a textarea and allow the IDs to be seperated by whitespace such as a new line as well as by commas.
Then I’d like to convert that into an int[] or throw an error if the string is bad: contains letters, decimals, etc.
But I’m having trouble working it out.
Can anyone give an example of how to do this?
I’d apprecuate any help.
If you use the
splitmethod, you can pass it a regular expression to split by. For example:This would return an array of strings, each containing a string representation of an integer. Furthermore, you can then use the
parseIntfunction to parse those string representations to integers, like so:You can see a full example with some jQuery helpers (specifically,
jQuery.map) here.