This discussion is archived
1 Reply Latest reply: Mar 10, 2007 12:06 PM by 807606 RSS

Regular expression for field validation

807606 Newbie
Currently Being Moderated
Hi all,

I am new to usage of regular expressions. I need to validate an input field :-
user is allowed to enter values in the following way :-
ex:- 1,3,7-10,15

- Here 7-10 means (7,8,9,10).
- every character other than those used here are invalid (i.e., no characters or any special characters other than comma (,) and hiphen (-) ). Only numbers, comma and hiphen are valid.

- and the string should not end with ',' or '-' (for example :- "1,2-,3," is not valid as '2-' and '3,' are not correct.

Could some one help me in doing this?

I will really appreciate If you can send me the code for this.

Thanks,
Hari
  • 1. Re: Regular expression for field validation
    807606 Newbie
    Currently Being Moderated
    I assume it's not necessarily only going to be 4 comma-separated parts, either.

    Think about it: the field's value is a series of comma-separated terms. That means that it can either be this:
    term
    or this:
    terms term
    Where "terms" is a sequence of one or more of this:
    term,
    And each term is going to be either this:
    number
    or this:
    number-number
    And number is a sequence of one or more numerals. You can specify that in regexps with this: \d+

    Good luck on your homework!

    If this isn't your homework, then I'd suggest not using regexps. The reason for this is that the requirements you gave are not likely to be complete. For example, in a give range, you probably want the second number to be larger than the first. That requires a numerical value comparison check which is tough to do in regexps. So I'd suggest parsing the sequence and doing more with it.